Wrapper classes are used to convert any data type into an object.
a wrapper class gets enclose around a data type and appears as an object.
for example:
int k = 100;
Integer it1 = new Integer(k);
The int data type k is converted into an object, it1 using Integer class.
to unwrap the object it1.
int m = it1.intValue();
System.out.println(m*m); // prints 10000
And now some programs
(1) Wrapper class Example: Primitive to Wrapper:
public class WrapperExample1{
public static void main(String args[]){
//Converting int into Integer
int a=20;
Integer i=Integer.valueOf(a);
Integer j=a;
System.out.println(a+" "+i+" "+j);
}}
OUPUT
20 20 20
(2) Wrapper class Example: Wrapper to Primitive:
public class WrapperExample2{
public static void main(String args[]){
//Converting Integer to int
Integer a=new Integer(3);
int i=a.intValue();
int j=a;
System.out.println(a+" "+i+" "+j);
}}
<b>OUPUT</b><br/>
3 3 3
0 Comment(s)