In this tutorial, I am going to explain what are wrapper classes in java. Why it is introduced and the concept of autoboxing and unboxing.
Wrapper Class
As the name suggests, a wrapper class wraps (encloses) around a primitive data type and gives it an object appearance. In simple words, we can convert primitive data types into an object using their corresponding wrapper class.
Wrapper classes are final and immutable.
How to create immutable class in java
For example:
1 2 3 4 5 6 7 8 |
//int and double are primitive types int a = 5; double b = 6.75; //Converted into an object by using their respective wrapper classes Integer intObj = Integer.valueOf(a); Double dbObj = Double.valueOf(b); |
Primitive Data Type
A primitive data type defines the size and type of variable values. In Java, we have eight primitive data types, which are as follows: