Java Reference
In-Depth Information
Self-Test Exercises
(continued)
23. What is the output produced by the following lines of program code?
int
n = 3;
n++;
System.out.println("n == " + n);
nāā;
System.out.println("n == " + n);
1.3
The Class
String
Words, words, mere words, no matter from the heart.
WILLIAM SHAKESPEARE,
Troilus and Cressida
There is no primitive type for strings in Java. However, there is a class called
String
that can be used to store and process strings of characters. This section introduces the
class
String
.
String
String Constants and Variables
You have already seen constants of type
String
. The quoted string
"Hello reader."
which appears in the following statement from Display 1.1, is a string constant:
System.out.println("Hello reader.");
A quoted string is a value of type
String
, although it is normally called an
object
of type
String
rather than a value of type
String
. An object of type
String
is a
sequence of characters treated as a single item. A variable of type
String
can name one
of these string objects.
For example, the following declares
blessing
to be the name for a
String
variable:
String blessing;
The following assignment statement sets the value of
blessing
so that
blessing
serves as another name for the
String
object
"Live long and prosper."
:
blessing = "Live long and prosper.";
The declaration and assignment can be combined into a single statement, as follows:
String blessing = "Live long and prosper.";