Page Index
Related
Introduction
In Java, a String
is a class, not a primitive data type. It represents a sequence of characters.
Unlike primitive data types, a String
is an object, which means it has methods you can use, support for character strings is implemented via java.lang.String class
.
How to create a string
In Java, String
is a special class and you can initialise a String
object using string literals directly, without the need to call a constructor.
Here’s an example of creating a String
using a constructor:
However, it’s important to note that it’s more common and simpler to create a String
object directly from a string literal, like this:
f string
Java you can use String.format()
method which works similarly to [[Java Output#printf|printf()
]]. It allows you to create a new string using a format string and arguments. Here is an example:
In this example, %s
is a placeholder for a string, and %d
is a placeholder for an integer. The variables name
and age
are inserted into these placeholders respectively.