Home >Java >javaTutorial >Wrapper Class in Java
Wrapper Class is an important class of java.lang library. Wrapper class objects create a wrapper for the primitive data types. While creating an object of the wrapper class, space is created in the memory where primitive data type is stored. Wrapper class provides some features for the conversion of the object to primitive data & primitive data to object, i.e. boxing/unboxing. Conversion from objects to primitive data & primitive data to an object takes place automatically. Primitive data type refers to the int, float, char, double, byte, etc.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Below given declaration shows how a Wrapper class works in the java program.
Example:
int i = 100;
In the below-given example, we can see i is an integer data type. Sometimes in the java integer needs to be passed as a type of object. In this case, we can use the wrapper class to convert an integer into an object.
Code:
Integer intVal = new Integer(i);
In the above-given syntax, we can see how a primitive data type is being converted to an object using an Integer class object. Also, we can say primitive data type is wrapped as an object.
Given below are few uses of the wrapper class:
On the basis of JavaAPI, the Wrapper class hierarchy keeps Object at the top of the different primitive classes. Number, Character & Boolean comes at the second level just after the Object. Byte, Short, Int, Long, Float, Double come under the Number data type at the third level.
Wrapper classes use the following two mechanisms Autoboxing & unboxing, for the conversion/wrapping of the data type or conversion of an object into the primitive data type.
Below are the different examples of Wrapper Class in Java:
In the below-given example, we can see how manual conversion takes place through wrapper class from int i to an object k.
Code:
import java.util.*; class WrapperExample { public static void main(String args[]){ int j=100; //converting int j to integer k as an object Integer k = new Integer(j); System.out.println(j + "\n" + k); } }
Output:
In the above-given example, we can see how conversion takes place explicitly.
In the below-given example, we can see this process of conversion sometimes takes place automatically, i.e. known as autoboxing.
Code:
import java.util.*; class AutoboxingUnboxingExample { public static void main(String args[]){ int j = 500; ArrayList<Integer> arrValues = new ArrayList(); arrValues.add(j); // autoboxing takes place implicitly System.out.println(arrValues.get(0)); } }
Output:
In the above-given example, the int value is converted to an object implicitly as an object. Further, this value can get from the ArrayList.
In this example, we will go through the implementation of Unboxing. Unboxing is the reverse process of Autoboxing.
Code:
import java.util.*; class AutoboxingUnboxingExample { public static void main(String args[]){ ArrayList<Integer> arrValues = new ArrayList(); arrValues.add(250); //unboxing here as int data type from Integer object int k = arrValues.get(0); //value printed is in primitive data type System.out.println(k); } }
Output:
In the above-given example, the ArrayList object field is converted into k primitive data type, i.e. int k.
The following given example have all the details of Autoboxing & Unboxing.
Code:
import java.util.*; class WrapperConversionExample { public static void main(String args[]){ int i = 15; float j = 9.6f; double k = 120.8; byte l = 1; //creating instance of Integer object Integer iObj = new Integer(i); //creating instance of Float object Float fObj = new Float(j); //creating instance of Double object Double dObj = new Double(k); //creating instance of Double object Byte bObj = new Byte(l); //value printed is in object System.out.println("Value as an Integer object > " + iObj); System.out.println("Value as a Float object > " + fObj); System.out.println("Value as a Double object > " + dObj); System.out.println("Value as a Byte object > " + bObj); //primitive data type from the object int m = iObj; float n = fObj; double o = dObj; byte p = bObj; //value printed is in primitive data type System.out.println("Value as an int primitive type > " + m); System.out.println("Value as a float primitive type > " + n); System.out.println("Value as a double primitive type > "+ o); System.out.println("Value as a byte primitive type > " + p); } }
Output:
In the above-given program, we can see the implementation of Wrapper classes. Wrapper classes are converting the primitive data type to object & object to the primitive data type. The wrapper class provides separate classes for each primitive data type.
Through the Wrapper classes, we can easily understand autoboxing & unboxing how conversion takes place from primitive to object & its vice versa, which can be easily understood through Wrapper classes. For each of the primitive data types, there is a dedicated class in java.
The above is the detailed content of Wrapper Class in Java. For more information, please follow other related articles on the PHP Chinese website!