Java Reference
In-Depth Information
TABLE 6-6 Some Members of the class Integer (continued)
public static int parseInt(String str)
//Returns the value of the number contained in str.
public String toString()
//Returns the int value, of the object, as a string.
public static String toString( int num)
//Returns the value of num as a string.
public static Integer valueOf(String str)
//Returns an Integer object initialized to the value
//specified by str.
The wrapper classes are contained in the package java.lang. As noted in Chapter 2, if
a class is contained in the package java.lang, to use that class in a program, it is not necessary
to explicitly import that class using the import statement. The system automatically imports
classes contained in the package java.lang. Therefore, to use the class Integer in a
program, it is not necessary to explicitly import this class using the import statement.
Consider the following statements:
Integer num;
//Line 1
num = new Integer(86)
//Line 2
The statement in Line 1 declares num to be a reference variable of type Integer .The
statement in Line 2 creates an Integer object, stores the value 86 in it, and then stores
the address of this object into num . (See Figure 6-16. Suppose that the address of the
Integer object is 1350 .)
1350
num
1350
86
FIGURE 6-16 The reference variable num and the object it points to
As you can see, the int value 86 is wrapped into an Integer object. Just like the class
String , the class Integer does not provide any method to change the value of an
existing Integer object. That is, Integer objects are immutable. (In fact, wrapper
class objects are immutable.)
Search WWH ::




Custom Search