X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Auto-boxing and Auto-unboxing :

Until 1.4 version of Java we can't provide primitive value in the place of wrapper objects and wrapper objects in the place of primitive. All the required conversion should be performed explicitly by the programmers.

Example :

class Example { public static void main(String[ ] args) { Boolean b = new Boolean(true); if(b) { System.out.println(b); } } }

Output :

Compile time error : Incompatible types found : Boolean required : boolean

Example :

class Example { public static void main(String[ ] args) { Boolean b1 = new Boolean(true); boolean b2 = b1.booleanValue(); if(b2) { System.out.println(b2); } } }

Output :

true

But from 1.5 version of Java, in the place of wrapper objects we can provide primitive value and in the place of primitive value we can provide wrapper objects. All the required conversions will be performed automatically by the compiler. These automatic conversions are called auto-boxing and auto-unboxing.


Auto-boxing :

Automatic conversion of primitive value to the wrapper object by compiler is called auto-boxing.

Example :

class Example { public static void main(String[ ] args) { Boolean b = true; Character c = 'a'; Integer i = 10; Long l = 10l; Float f = 10.5f; Double d = 10.5; System.out.println(b); System.out.println(c); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); } }

Output :

true a 10 10 10.5 10.5

Auto-unboxing :

Automatic conversion of wrapper class to the primitive type by compiler is called auto-unboxing.

Example :

class Example { public static void main(String[ ] args) { boolean b = new Boolean(true); char c = new Character('a'); int i = new Integer(10); long l = new Long(10l); float f = new Float(10.5f); double d = new Double(10.5); System.out.println(b); System.out.println(c); System.out.println(i); System.out.println(l); System.out.println(f); System.out.println(d); } }

Output :

true a 10 10 10.5 10.5

Note :

Auto-boxing concept internally implemented by using valueOf() method.

Auto-unboxing concept internally implemented by using xxxValue() method.

Example :

class Example { static Integer I = 10; public static void main(String[ ] args) { int i = I; algo(i); } public static void algo(Integer I) { int j = I; System.out.println(j); } }

Output :

10

Note :

Due to auto-boxing and auto-unboxing from 1.5 version of Java, there is no difference between primitive value and wrapper object. We can use interchange of each other in any case.

Example1 :

class Example { static Integer I = 10; public static void main(String[ ] args) { int i = I; System.out.println(i); } }

Output :

10

Example2 :

class Example { static Integer I; public static void main(String[ ] args) { int i = I; System.out.println(i); } }

Output :

Runtime error : NullPointerException

Example3 :

class Example { public static void main(String[ ] args) { Integer a = 10; Integer b = a; a++; System.out.println(a); System.out.println(b); System.out.println(a == b); } }

Output :

11 10 false

Example4 :

class Example { public static void main(String[ ] args) { Integer a = new Integer(10); Integer b = new Integer(10); System.out.println(a == b); } }

Output :

false

Example5 :

class Example { public static void main(String[ ] args) { Integer a = new Integer(10); Integer b = 10; System.out.println(a == b); } }

Output :

false

Example6 :

class Example { public static void main(String[ ] args) { Integer a = 10; Integer b = 10; System.out.println(a == b); } }

Output :

true

Example7 :

class Example { public static void main(String[ ] args) { Integer a = 100; Integer b = 100; System.out.println(a == b); } }

Output :

true

Example8 :

class Example { public static void main(String[ ] args) { Integer a = 1000; Integer b = 1000; System.out.println(a == b); } }

Output :

false

Example9 :

class Example { public static void main(String[ ] args) { Float a = 10.5f; Float b = 10.5f; System.out.println(a == b); } }

Output :

false

Example10 :

class Example { public static void main(String[ ] args) { Boolean a = false; Boolean b = false; System.out.println(a == b); } }

Output :

true

Conclusion :

  • By auto-boxing if compiler require to create object then it won't create the object immediately, first it will check that there is any object already created.

  • If it is already created then it will reuse existing object instead of creating new object.

  • If it is not already created then only a new object will be created.

  • For reusing an object the wrapper class should be in following range :

Wrapper class          Range
Boolean                Always
Character              0 to 127
Byte                   Always
Short                  -128 to 127
Integer                -128 to 127
Long                   -128 to 127
  • Except the above wrapper classes with such range, in all other cases it is compulsory to create a new object.

Overloading with respect to auto-boxing, widening and var_arg() methods :-


Example1 :

class Example { public static void algo(long l) { System.out.println("Widening"); } public static void algo(Integer I) { System.out.println("Auto-boxing"); } public static void main(String[ ] args) { int a = 10; algo(a); } }

Output :

Widening Note :- Widening dominates over auto-boxing during method call.

Example2 :

class Example { public static void algo(long l) { System.out.println("Widening"); } public static void algo(int... i) { System.out.println("Var_arg()"); } public static void main(String[ ] args) { int a = 10; algo(a); } }

Output :

Widening Note :- Widening dominates over var_arg() during method call.

Example3 :

class Example { public static void algo(Integer I) { System.out.println("Auto-boxing"); } public static void algo(int... i) { System.out.println("Var_arg()"); } public static void main(String[ ] args) { int a = 10; algo(a); } }

Output :

Auto-boxing Note :- Auto-boxing dominates over var_arg() during method call.
  • In general, var_arg() method will get least priority (i.e. when no other method matched with the given argument in the method call then only var_arg() method will be executed).

While resolving overloaded method, compiler will always keep the precedence in the following order :

  • Widening




  • Auto-boxing




  • Var_arg()




Example :

class Example { public static void algo(Long l) { System.out.println("Auto-boxing"); } public static void main(String[ ] args) { int a = 10; algo(a); } }

Output :

Compile time error : method algo in class Example cannot be applied to given types; required: Long found: int reason: actual argument int cannot be converted to Long by method invocation conversion Note :- Here, int is converted into Integer using auto-boxing and then Integer is converted into Long using widening. It is not allowed in Java.
X

invocation :

It refers to the action of invoking (call on) someone or something.

Example :

class Example { public static void algo(Object o) { System.out.println("Object"); } public static void main(String[ ] args) { int a = 10; algo(a); } }

Output :

Object Note :- Here, int is converted into Integer using auto-boxing then Integer is converted into Object using widening. It is allowed in Java.


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.