Java Reference
In-Depth Information
to a more specific type is called downcasting . Only objects that actually are
instances of a more specific class should be downcast. For example, any String
also is an Object, but the reverse is not true. In this case, the Object returned
from the get() method can be downcast safely to a String, because String objects
originally were stored in the ArrayList. If you do not know that an object, refer-
enced as a more general parent class, also is a member of a child class, use the
instanceof operator before trying to downcast the object, as in the code
if(pswd instanceof String) ...
The resulting String, which is the current password, can be compared to the
password value provided by the user program by using the equals() method, as
shown in line 179 of Figure 9-32 on page 570. The equals() method compares
two Strings and returns a boolean value — true or false. If the value is false, then
an exception is thrown with the message, Password is invalid (line 180). Note
that the String equals() method requires only an Object as an argument, so a
downcast would not be required to use it. The method returns true, however, only
if the Object actually is a String with a matching character sequence. Other String
comparison methods are available that take an Object, a String, or a StringBuffer
as an argument.
The remaining methods for the Password class are not part of the public
interface; that is, they are used only for internal purposes and are not made
available for public use. Before coding these methods, it is important to under-
stand the String and StringBuffer classes because they are used in these methods.
Understanding the String and StringBuffer Classes
Most applications have a need to deal with characters grouped together, such as
a name or some other textual value, rather than as individual, separate charac-
ters. Many programming languages have features for dealing with such group-
ings of characters, or strings, inherent in the language itself. Java, however, does
not have a primitive data type for strings. Rather, some central classes, or core
classes , are provided with the language that offer functionality for dealing with
this type of data. The String and StringBuffer classes are the primary core classes
in Java for manipulating string values.
Using the String Class
The String class is the Java class that supports immutable string values —
that is, their values cannot be changed because String objects are constants. Recall
that String literals, which are designated by characters within double quotes such
as “abc123”, are implemented as instances of the String class. When it encounters
that String literal, Java creates a String object whose value is abc123.
You also can create String objects using the new keyword and a constructor,
as in String phrase = new String(“Java is fun”); which assigns the ini-
tial value, Java is fun, to the string. If you assign a new value to a String, it does
not replace the old value; the String reference simply refers to the new String, and
the old String is abandoned to be removed later by the garbage collector of the
Java Virtual Machine (JVM). The garbage collector is a routine provided by the
JVM that frees previously allocated memory for objects which are no longer in
use. Strings have a number of useful methods, including methods for examining
Search WWH ::




Custom Search