Java Wrapper Class
Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type.
The type wrapper classes are Double, Float, Long, Integer, Short, Byte, Character, and Boolean. They offer a wide range of methods that allows us to integrate primitive data type into Java’s object hierarchy completely.
Uses of Wrapper class
- It is used to convert the primitive data type into objects.
- Classes in “Java.util” package deals only with objects, so in this the wrapper classes plays its role.
- We convert the primitive value to object using wrapper class because we need to convert the objects into streams to perform the serialization.
- Synchronization in Java works with objects in multithreading. Therefore it is required to convert the value into objects.
- The Collection framework in Java works with objects only.
The corresponding type wrapper class for the primitive type is in below table:
Primitive Type | Wrapper class |
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
As you can see in the above table, the primitive types are written in lower case. But the wrapper class’s first letter is capital. So always use them as given above. Each Wrapper classes are briefly explained below:
The Numeric Type Wrappers
The numeric type wrappers are mostly used. These are Byte, Short, Integer, Long, Float, and Double. All the type wrappers inherit the abstract class Number. Below are the methods that are declared by Number class which returns the value of an object in different number formats.
bytebyteValue()
doubledoubleValue()
floatfloatValue()
intintValue()
longlongValue()
shortshortValue()
Character Type Wrapper
It is a wrapper class for char. The constructor for Character is Character(char ch).
Here, ch denotes the character that will be wrapped by the Character object being created. To get the char value contained in a Character object, we need to call charValue() as show here:
charcharValue().
Boolean Type Wrapper
It is a wrapper class for boolean values. The constructor for boolean are:
Boolean(booleanboolValue)-boolValue must be either true or false.
Boolean(String boolString)-if boolString contains the string “true”, then the new Boolean object will be true else it will be false.
Autoboxing and Auto-unboxing
From JDK 5, Java added two other features: autoboxing and auto-unboxing. The process of automatically encapsulating primitive type into its equivalent type wrapper whenever an object type is needed is called Autoboxing. Whereas, the process by which the value of a boxed object is automatically unboxed from a type wrapper, when its value is needed is called as Auto-unboxing.
It also helps prevent errors by making ease of coding for several algorithms and removes the vulnerabilities of manually boxing and unboxing.
Example to demonstrate autoboxing/unboxing :
Class AutoboxDemo { public static void main (String args[]) { Integer first = 10; //autoboxing an int inti = first; //auto-unboxing System.out.println(i + " " + first); } } classWrapperDemo { publicstaticvoid main(String args[]) { byte a = 10; int b = 20; float c = 20.5f; char e='a'; double d = 25.5; //autoboxing Byte byteobj = newByte(a); Integer intobj = newInteger(b); Double doubleobj = newDouble(d); Character charobj=e; Float floatobj = new Float(c); //printing the values from objects System.out.println("---Values of objects (printing as objects)---"); System.out.println("Byte object-byteobj: " + byteobj); System.out.println("Integer object-intobj: " + intobj); System.out.println("Float object-floatobj: " + floatobj); System.out.println("Double object-doubleobj: " + doubleobj); System.out.println("Character object-charobj: " + charobj); //un-boxing bytebv = byteobj; int iv = intobj; floatfv = floatobj; double dv = doubleobj; char cv = charobj; // printing the values from data types System.out.println("---Printing primitive values---"); System.out.println("byte value, bv: " + bv); System.out.println("int value, iv: " + iv); System.out.println("float value, fv: " + fv); System.out.println("double value, dv: " + dv); System.out.println("char value, cv: " + cv); } }
Output:
---Values of objects (printing as objects)--- Byte object-byteobj: 10 Integer object-intobj: 20 Float object-floatobj: 20.5 Double object-doubleobj: 25.5 Character object-charobj: a ---Printing primitive values--- byte value, bv: 10 int value, iv: 20 float value, fv: 20.5 double value, dv: 25.5 char value, cv: a