Next:
StringBuffer class
Up:
Strings
Previous:
Strings
Contents
String objects
Java provides a
String
class to deal with sequences of characters.
String username = "Fred Flinstone";
The
String
class provides a variety of methods to operate on String objects.
The
equals()
method is used to compare Strings.
The
length()
method returns the number of characters in the String.
The
+
operator is used for String concatenation.
String
objects are read-only (also called
immutable
).
In the example below, the second assignment gives a new value to the object reference
str
, not to the contents of the string:
str = "Fred"; str = "Barney"
An array of
char
is not a
String
.
Next:
StringBuffer class
Up:
Strings
Previous:
Strings
Contents