Autoboxing
Autoboxing is a
process when primitive data types in Java automatically converts into its
corresponding wrapper class object. Like, converting an char to Character, int
to an Integer or double to a Double.
Autoboxing
Example :
import java.util.*;
class Autoboxing
{
public static void main(String args[])
{
Integer num=5; //Invoke Integer.valueOf()
Double D=5.4; //Invoke Double.valueOf()
Float F=5.0F; //Invoke Float.valueOf()
Character ch='x'; //Character.valueOf()
ArrayList
N= new ArrayList();
System.out.println("Integer
: "+num);
System.out.println("Double : "+D);
System.out.println("Float :
"+F);
System.out.println("Character
: "+ch);
N.add(5);
N.add(9);
N.add(11);
N.add(6);
N.add(7);
System.out.print("\nArrayList : ");
System.out.print("\nArrayList : ");
for(int i=0;i<5;i++)
{
System.out.print(N.get(i)+" ");
}
}
}
}
}
}
System.out.print("\nArrayList : ");
for(int i=0;i<5;i++)
{
System.out.print(N.get(i)+" ");
}
}
}
}
}
}
Unboxing
When automatic
conversion taken place for an object of a wrN.add(7)apper type to its corresponding
primitive value is known as unboxing.
Unboxing Example
:
public class
Unboxing
{
public static void main(String args[])
{
Integer
NUM=10;
Double D=12.5;
int num=NUM;
double d=D;
System.out.println("Integer
: "+NUM+" int : "+num);
System.out.println("Double
: "+D+" double : "+d);
}
}
No comments:
Post a Comment