Java Reference
In-Depth Information
In the next section, you will see in greater detail how to supply method inputs and
obtain method outputs.
Let us look at another method of the
String
class. When you apply the
toUpperCase
method to a
String
object, the method creates another
String
object that contains the characters of the original string, with lowercase letters
converted to uppercase. For example, the sequence of statements
String river = ÐMississippiÑ;
String bigRiver = river.toUpperCase();
sets
bigRiver
to the
String
object
ÐMISSISSIPPIÑ
.
When you apply a method to an object, you must make sure that the method is defined
in the appropriate class. For example, it is an error to call
System.out.length();
// This method call is an error
The
PrintStream
class (to which
System.out
belongs) has no
length
method.
Let us summarize. In Java, every object belongs to a class. The class defines the
methods for the objects. For example, the
String
class defines the
length
and
toUpperCase
methods (as well as other methodsȌyou will learn about most of
them in
Chapter 4
). The methods form the public interface of the class, telling you
what you can do with the objects of the class. A class also defines a private
implementation, describing the data inside its objects and the instructions for its
methods. Those details are hidden from the programmers who use objects and call
methods.
The public interface of a class specifies what you can do with its objects. The
hidden implementation describes how these actions are carried out.
Figure 4
shows two objects of the
String
class. Each object stores its own data
(drawn as boxes that contain characters). Both objects support the same set of
methodsȌthe interface that is specified by the
String
class.
S
ELF
C
HECK
6.
How can you compute the length of the string
ÐMississippiÑ
?