Java Reference
In-Depth Information
These objects belong to different classes. The System.out object belongs to the
class PrintStream . The ÐHello, World!Ñ object belongs to the class
String . A class specifies the methods that you can apply to its objects.
You can use the println method with any object that belongs to the
PrintStream class. System.out is one such object. It is possible to obtain other
objects of the PrintStream class. For example, you can construct a
PrintStream object to send output to a file. However, we won't discuss files until
Chapter 11 .
A class defines the methods that you can apply to its objects.
Just as the PrintStream class provides methods such as println and print for
its objects, the String class provides methods that you can apply to String
objects. One of them is the length method. The length method counts the number
of characters in a string. You can apply that method to any object of type String .
For example, the sequence of statements
String greeting = ÐHello, World!Ñ;
int n = greeting.length();
sets n to the number of characters in the String object ÐHello, World!Ñ . After
the instructions in the length method are executed, n is set to 13. (The quotation marks
are not part of the string, and the length method does not count them.)
The length methodȌunlike the println methodȌrequires no input inside the
parentheses. However, the length method yields an output, namely the character
count.
38
39
Figure 4
A Representation of Two String Objects
Search WWH ::




Custom Search