Wrapper classes in java are nothing but the classes which are used to convert the primitive data types into Objects.
As their name suggests, they are used to wrap the primitive data types into an Object type. The primitive data types are not objects, so they do not belong to any class.
Collections only store and support objects, so it is required to convert the primitive type to object first, which we can do by using wrapper classes.
All the wrapper classes are subclasses to Object Class, All the number-related classes are sub-class to Number class. The Number class is an abstract class in Java.
Note: There are four other subclasses of Numbers that are not discussed here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications.
Boolean wrapper class converts the boolean primitive data into Boolean Object.
All the number related classes are inherited from the Number class.
Character wrapper class converts the character primitive values into a character object.
Byte wrapper class converts the byte value into Byte Object
Integer class converts the primitive value into Integer values; remaining wrapper classes also converts primitive values into respective Object value.
We will be discussing the methods present in the Integer class. Almost all the remaining classes have similar methods only.
The Integer class has two constructors: 1. Integer(int i) : accepts the integer parameter and creates the object for the int.
Integer intObj1 = new Integer(10);
2. Integer(String s) : accepts String parameter, converts the accepted string parameter into int and creates Integer object for the int.
Integer intObj2 = new Integer("89");
Instead of using the above two forms, it is always better to use the below way of creating the object and which takes less memory.
Integer i = Integer.valueOf("100");
Below are few non-static methods present in the Integer class
Returns the value of this Integer as a byte.
Compares two Integer objects numerically.
Returns the value of this Integer as an int.
Returns a String object representing this Integer's value. Below are few static methods present in the Integer class.
Parses the string argument as a signed decimal integer.
Returns an Integer object holding the value of the specified String.
Returns a String object representing the specified integer.
Reasons to use Number class than primitive values :
There are three reasons that you might use a Number object rather than a primitive
MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.Features of the Java wrapper Classes
typeValue() method. This method returns the value of the object as its primitive type.
Reasons for using Wrapper classes :
The process of converting the primitive data type into an Object using the Wrapper class is called Boxing. For example, converting int to Integer.
Auto-boxing is nothing but converting primitive data into Objects automatically; this is the reason we are able to store our primitive values in the Collections.
The Java compiler can directly convert the primitive types into corresponding objects. This process is known as auto-boxing. Auto-boxing happens-
public static void autoboxMethod(Integer num){
System.out.println(num);
}
public static void main(String[] args) {
//passed int (primitive type) would be converted to Integer object at Runtime
//autoboxing
autoboxMethod(14);
}
Output-
14
public static void main(String args[]){
int a=14;
//autoboxing
Integer obja=new Integer(a);
//autoboxing
Integer b=5;
System.out.println(obja);
System.out.println(b);
}
Output-
14
5
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
//autoboxing
list.add(1);
list.add(2);
System.out.println("ArrayList: " + list);
}
Output-
ArrayList: [1, 2]
Unboxing is the process of converting the primitive values which are wrapped as Objects into primitive values.
Unboxing happens when-
public static void unboxMethod(int num){
System.out.println(num);
}
public static void main(String[] args) {
Integer num = new Integer(14);
//passed Integer wrapper class object
//would be converted to int primitive type at Runtime
unboxMethod(num);
}
Output-
14
public static void main(String args[]){
Integer i=new Integer(14);
//unboxing int type to Integer object
int a=i;
System.out.println(a);
}
Output-
14
In the below example, the object is automatically converted into the primitive type int and assigned to the variable a-
public static void main(String[] args) {
ArrayList < Integer > list = new ArrayList < > ();
//autoboxing
list.add(1);
list.add(2);
System.out.println("ArrayList is " + list);
// unboxing
//here the get() method returns the object at the index 0
int a = list.get(0);
System.out.println("Value at index 0 is " + a);
}
Output-
ArrayList is [1, 2]
Value at index 0 is 1
Auto-Unboxing is nothing but converting Objects into primitive values automatically. With respect to java boxing and Unboxing occurs automatically, so JVM uses boxing and unboxing auto-boxing and auto-unboxing to store the primitive values in the list and to retrieve from the list.
import java.util.ArrayList;
import java.util.List;
public class IntegerExample {
public static void main(String[] args) {
// below is the process of converting int -> Integer
// also known as boxing
int i = 10; // primitive value
Integer intObj = new Integer(10);
// below is example for auto-boxing
List al = new ArrayList();
// syntax of adding a value to list
// al.add(Object e);
// but in place of Object we are able to store the int
// int is not stored as int but as Integer
//and then converted into Object class object
al.add(i);
// unboxing
intObj.intValue();
// on below line int stored as object is Un-boxed and became as int
// this is called as auto-unboxing
System.out.println(al.get(0));
}
}
Output-
10
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.