Wrapper class in java
Java is an object-oriented language and can view everything as an object. A simple file can be treated as an object , an address of a system can be seen as an object , an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an object (with wrapper classes). This tutorial discusses wrapper classes. Wrapper classes are used to convert any data type into an object.
The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language. For example, upto JDK1.4, the data structures accept only objects to store. A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes.
What are Wrapper classes?
As the name says, a wrapper class wraps (encloses) around a data type and gives it an object appearance. Wherever, the data type is required as an object, this object can be used. Wrapper classes include methods to unwrap the object and give back the data type. It can be compared with a chocolate. The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. The user takes the chocolate, removes and throws the wrapper and eats it.
Observe the following code.
int k = 100;
Integer int1 = new Integer(k);
The int data type k is converted into an object, int1 using Integer class. The int1 object can be used in Java programming wherever k is required an object.
The following code can be used to unwrap (getting back int from Integer object) the object int1.
int m = int1.intValue();
System.out.println(m*m); // prints 10000
intValue() is a method of Integer class that returns an int data type.
Importance of Wrapper classes :
- To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
- To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
Features of the Java wrapper Classes :
- Wrapper classes convert numeric strings into numeric values.
- The way to store primitive data in an object.
- The valueOf() method is available in all wrapper classes except Character
- All wrapper classes have typeValue() method. This method returns the value of the object as its primitive type.
Why these two types exist :
At the virtual machine level, it’s because primitive types are represented very differently in memory compared to reference types like java.lang.Object and its derived types. Primitive int in Java for example is just 4 bytes in memory, whereas an Object takes up at minimum 8 bytes by itself, plus another 4 bytes for referencing it. Such design is a simple reflection of the fact that CPUs can treat primitive types much more efficiently.
But for programmers, such distinction adds some undesirable cognitive overhead (e.g., can’t use int and float in collections.) In fact, it’s quite possible to do a language design by hiding that distinction — many scripting languages do this, and CLR does that. Starting 1.5, Java does that, too. This is achieved by letting the compiler silently insert necessary conversion between primitive representation and Object representation (which is commonly referred to as boxing/unboxing.)