What is AutoBoxing and AutoUnboxing in Java

0
29
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

What is AutoBoxing and AutoUnboxing in Java
What is AutoBoxing and AutoUnboxing in Java

What is AutoBoxing and AutoUnboxing in Java?

In my previous post I have talked about the datatypes here. In this post we will see the conversion between them.Java has introduced automatic conversion between primitive types and the corresponding wrapper class.So if we use a value of type int in a context that requires an object of type Integer,the int will automatically wrapped in an Integer object.


int a=22; is equivalent to
Integer a new Integer(22);

This feature is called autoboxing. Similarly as variable a refers to an object of type Integer we can use ‘a’ in the numerical expression like 3*a.
The integer value in side ‘a’ automatically unboxes to int and complete the operation.This feature is called autounboxing.

Autoboxing and Autounboxing can be applied to Byte,Short,Long,Double,Float data types and method calls.So the autoboxing and unboxing feature facilitates the process of handling primitive datatypes in Collections.We can use this feature to convert primitive types to corresponding wrapper classes automatically.The compiler generates a code implicitly to convert primitive type to the wrapper class. and vice versa.


Double d_object=98.42;
Double d_primitive=d_object.doubleValue();
//same can be written as
Double d_object=98.42;
Double d_primitive=d_object;
 

java puts restrictions to perform the following conversions:

  1. Convert from null type to any primitive type.
  2. Convert to the null type other than the identified conversion.
  3. Convert from any class type c to any array type if c is not object.

A general code for Collection:


import java.util.*;
public class autoUnboxingTest{

     public static void main(String []args){
        Stack myStack=new Stack();
        //Container stack can take only Object hence converting
        myStack.push(new Integer(10));
        myStack.push(new Integer(20));
        Integer num1=(Integer)myStack.pop();
        Integer num2=(Integer)myStack.pop();
        int sum=num1.intValue()+num2.intValue();
        System.out.println(sum);
     }
}
 

output of the code:
$javac autoUnboxingTest.java
Note: autoUnboxingTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$java -Xmx128M -Xms16M autoUnboxingTest
30
Same can be written as:


import java.util.*;
public class autoUnboxingTest{

     public static void main(String []args){
        Stack<Integer> myStack=new Stack<Integer>();
        //autoboxing
        myStack.push(10);
        myStack.push(20);
        //autounboxing
        int sum=myStack.pop()+myStack.pop();
        System.out.println(sum);
     }
}
 

$javac autoUnboxingTest.java
$java -Xmx128M -Xms16M autoUnboxingTest
30

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here