Wednesday, January 28, 2009

Wrapper Classes  


Each of Java's eight primitive data types has a class dedicated to it. These are known as wrapper 
classes, because they "wrap" the primitive data type into an object of that class. So, there is an 
Integer class that holds an int variable, there is a Double class that holds a double 
variable, and so on. The wrapper classes are part of the java.lang package, which is imported 
by default into all Java programs. The following discussion focuses on the Integer wrapper 
class, but applies in a general sense to all eight wrapper classes. Consult the Java API 
documentation for more details. 
The following two statements illustrate the difference between a primitive data type and an object 
of a wrapper class: 
int x = 25; 
Integer y = new Integer(33); 
The first statement declares an int variable named x and initializes it with the value 25. The 
second statement instantiates an Integer object. The object is initialized with the value 33 and 
a reference to the object is assigned to the object variable y. The memory assignments from 
these two statements are visualized in Figure 1. 

Clearly x and y differ by more than their values: x is a variable that holds a value; y is an object 
variable that holds a reference to an object. As noted earlier, data fields in objects are not, in 
general, directly accessible. So, the following statement using x and y as declared above is not 
int z = x + y; // wrong! 
The data field in an Integer object is only accessible using the methods of the Integer class.  
One such method — the intValue() method — returns an int equal to the value of the 
object, effectively "unwrapping" the Integer object: 
int z = x + y.intValue(); // OK! 
Note the format of the method call. The intValue() method is an instance method, because it 
is called through an instance of the class — an Integer object. The syntax uses dot notation, 
where the name of the object variable precedes the dot. 
Some of Java's classes include only instance methods, some include only class methods, some 
include both. The Integer class includes both instance methods and class methods. The class 
methods provide useful services; however, they are not called through instances of the class.  
But, this is not new. We met a class method of the Integer class earlier. The statement  
int x = Integer.parseInt("1234"); 
converts the string "1234" into the integer 1,234 and assigns the result to the int variable x.  
The method parseInt() is a class method. It is not called through an instance of the class.  

Although dot notation is used above, the term "Integer." identifies the class where the method 
is defined, rather than name of an Integer object. This is one of the trickier aspects of 
distinguishing instance methods from class methods. For an instance method, the dot is preceded 
by the name of an object variable. For a class method, the dot is preceded by the name of a class.  
It is interesting that the parseInt() method accepts a String argument and returns an int.  
Neither the argument nor the returned value is an Integer object. So, the earlier comment that 
class methods "provide useful services" is quite true. They do not operate on instance variables; 
they provide services that are of general use. The parseInt() method converts a String to 
an int, and this is a useful service — one we have called upon in previous programs. 
Another useful Integer class method is the toString() method. The following statement 
String s = Integer.toString(1234); 
converts the int constant 1,234 to a String object and returns a reference to the object. We 
have already met this method through the following shorthand notation: 
String s = "" + 1234; 

The most common methods of the Integer wrapper class are summarized in Table 1. Similar 
methods for the other wrapper classes are found in the Java API documentation. 
Table 1. Methods of the Integer Wrapper Class 
Method Purpose 
Integer(i)  constructs an Integer object equivalent to the integer i 
Integer(s)  constructs an Integer object equivalent to the string s 
Class Methods 
parseInt(s)  returns a signed decimal integer value equivalent to string s 
toString(i)   returns a new String object representing the integer i 
Instance Methods 
byteValue()   returns the value of this Integer as a byte 
doubleValue()   returns the value of this Integer as an double 
floatValue()   returns the value of this Integer as a float 
intValue()   returns the value of this Integer as an int 
longValue()   returns the value of this Integer as a long 
shortValue()   returns the value of this Integer as a short 

In the descriptions of the instance methods, the phrase "this Integer" refers to the instance 
variable on which the method is acting. For example, if y is an Integer object variable, the 
expression y.doubleValue() returns the value of "this Integer", or y, as a double.

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories

0 comments: to “ Wrapper Classes

Extension Factory Builder