In this post we will understand and learn what is AutoBoxing and UnBoxing in java. Until Java 1.4 version in the place wrapper object we are not allowed to place primitive values.
Example:
ArrayList al=new ArrayList();
al.add(10); -------> Compile time error in 1.4 version
Integer i=new Integer(10);
i.add(al); --------> after converting No compile time error for above example
Similarly in the place of primitive we are not allowed to place wrapper objects.
Example:
Boolean b=new Boolean("true");
if(b)
{
System.out.println("hi");
}
else
{
System.out.println("java");
}
It generates Compile time error says:
incompatible type found: Boolean
required : Boolean
if you rewrite preceding code like below then it is valid:
boolean b=b.booleanValue();
if(b)
{
System.out.println("hi");
}
else
{
System.out.println("java");
}
But from Java 1.5 version on wards we can pass primitives in the place of objects and wrapper objects in the place of primitives. "The required conversion will take by compiler automatically" this is nothing but AutoBoxing and AutoUnBoxing.
AutoBoxing:
The automatic conversion from primitive to wrapper object by the compiler is called AutoBoxing.
Example: Integer i=10;
//compiler automatically converts primitive to Integer object form
AutoUnBoxing:
The automatic conversion from wrapper object to primitive type by the compiler is called AutoUnBoxing.
Example:
int i=new Integer(10);//compiler automatically converts Integer object to int primitive
From java 1.5 version on words there is no difference between wrapper object and primitive.we can use interchangeably.
EXAMPLE 1:
class Demo
{
public static Integer I = 10;
public static void main(String[] args)
{
int i = I;
System.out.println(i);
}
}
Output:
Now we will compare AutoBoxing Vs var...args method:
Compiler gives the precedence for the autoboxing over var-arg method. That means var-arg will always get least priority if there is no other method then only var-arg method will get a chance.
The following example demonstrate this concept:
Output:
From the above example we aware that first priory should take AutoBoxing only.
I hope you enjoy this post and share this into social websites and also give comments on this post .
Example:
ArrayList al=new ArrayList();
al.add(10); -------> Compile time error in 1.4 version
Integer i=new Integer(10);
i.add(al); --------> after converting No compile time error for above example
Similarly in the place of primitive we are not allowed to place wrapper objects.
Example:
Boolean b=new Boolean("true");
if(b)
{
System.out.println("hi");
}
else
{
System.out.println("java");
}
It generates Compile time error says:
incompatible type found: Boolean
required : Boolean
if you rewrite preceding code like below then it is valid:
boolean b=b.booleanValue();
if(b)
{
System.out.println("hi");
}
else
{
System.out.println("java");
}
But from Java 1.5 version on wards we can pass primitives in the place of objects and wrapper objects in the place of primitives. "The required conversion will take by compiler automatically" this is nothing but AutoBoxing and AutoUnBoxing.
AutoBoxing:
The automatic conversion from primitive to wrapper object by the compiler is called AutoBoxing.
Example: Integer i=10;
//compiler automatically converts primitive to Integer object form
AutoUnBoxing:
The automatic conversion from wrapper object to primitive type by the compiler is called AutoUnBoxing.
Example:
int i=new Integer(10);//compiler automatically converts Integer object to int primitive
From java 1.5 version on words there is no difference between wrapper object and primitive.we can use interchangeably.
EXAMPLE 1:
class Demo
{
public static Integer I = 10;
public static void main(String[] args)
{
int i = I;
System.out.println(i);
}
}
Output:
Now we will compare AutoBoxing Vs var...args method:
Compiler gives the precedence for the autoboxing over var-arg method. That means var-arg will always get least priority if there is no other method then only var-arg method will get a chance.
The following example demonstrate this concept:
Output:
From the above example we aware that first priory should take AutoBoxing only.
I hope you enjoy this post and share this into social websites and also give comments on this post .
No comments:
Post a Comment