J2SE 5.0 introduced a new feature called autoboxing and unboxing that automatically converts between the primitive data types and their wrapper classes. To understand this feature and appreciate its importance, we need to understand the previously used wrapper classes.
As we know, Java is highly object oriented. But what about its primitive data types? Are these objects? The answer to this question is no; the primitive data types in Java are not classes. Therefore, we lose the advantages that we have with classes when using the primitive data types.
Here are some of the disadvantages:
- The simple data types are not part of the Object hierarchy and therefore cannot be used as objects, as we would do with any other class in the Object hierarchy.
- We cannot pass a primitive data type to a Java method by reference; it is always sent by value.
- Two different methods in our program cannot refer to the same instance of a simple data type.
- Some classes can use only objects and cannot use simple data types. For example, the Vector class we covered previously cannot hold a list of numbers.
To overcome this and other limitations, Java provides type wrappers for all its primitive data types.
- The Integer class wraps an int data type,
- the Float class wraps a float data type,
- and so on…..
Each of the primitive data types is wrapped into a class having the same name as the data type but with the first letter capitalized. The exceptions are char, for which the wrapper class is called Character, and the int type, for which the wrapper class is Integer. All these wrapper classes are derived from the Number class.
Important Fields and methods provided by Wrapper Classes
Let’s go over a few important fields and methods of these wrapper classes that we will use frequently.
- The MAX_VALUE and MIN_VALUE fields define the maximum and the minimum values for the data type being wrapped.
- The parseInt method (and the parseXxx methods for other data types) takes a string argument with an optional radix argument and returns the corresponding data type to the caller after converting the value specified in its argument.
- The valueOf method takes a primitive data type as its argument and returns an object of the corresponding wrapper class.
- The toString method returns the string representation of the value of the wrapped primitive data type.
- These classes also provide a xxxValue() method that returns the wrapped primitive type.
For example, the booleanValue method of the class Boolean will return a boolean data type, and the intValue method of the class Integer will return an int variable.
For simple tasks, primitives are easy to use because we can use many of the operators on them, rather than calling methods.
Here are some features of these wrapper classes worth noting:
- All the methods of the wrapper classes are static
- A wrapper class does not contain constructors
- And the objects of the wrapper classes are immutable, which means that once a value is assigned to a wrapper class object, it cannot be changed.