Friday, 31 March 2017

AutoBoxing and UnBoxing in Java

From Java 5 two new features were added Autoboxing and Unboxing where Autoboxing automates the wrapping of primitive data types into the wrapper class for that particular data type when an object instead of primitive type is needed, Unboxing does the exact opposite that is it gets the value out of a wrapped object automatically.

As example –

Before this features autoboxing was added wrapping a primitive int to its corresponding wrapper class Integer would have looked like –

int num = 25;
Integer i = Integer.valueOf(num);

Now with this feature you can directly assign value to a wrapper class –

Integer i = 25;

Now valueOf() method call will be done by the compiler behind the scene.

Same way for auto unboxing if unboxing; a wrapped object previously meant calling the method intValue(), longValue(), doubleValue() based on the type

Integer i = new Integer(10);
int num = i.intValue();

Now it can be done directly –

Integer i = new Integer(10);
int num = i;

Or for float type

Float f = new Float(56.78);
float fNum = f;

Benefits of Autoboxing and Unboxing

As you have already seen with this feature you don’t need to wrap primitive types or unwrap them manually. That is a great help with the other feature generics also added in Java 5. Since generics work only with object so having the auto facility to box and unbox greatly simplifies the coding.

As the Collection classes also work only with objects so it makes working with collections easy too.

As example –

List<Integer> li = new ArrayList<>();
for (int i = 1; i < 10; i++){
    li.add(i);
}

Even though you have a List which stores objects of type Integer you can still add primitive int directly to List because of Autoboxing. Earlier when the primitive ints were added to the list, behind the scene those values were wrapped into the Integer objects and then added to the ArrayList.

Same way unboxing will also happen automatically.

int elem = li.get(0);
System.out.println("elem " + elem);

When you are trying to get the first element out of the list and assigned it to an int variable, then Integer is unboxed to get the int value out of the wrapped Integer class and that value is assigned to an int variable elem.

More examples of autoboxing and unboxing

We have already seen few examples of assignments where primitive types are autoboxed into wrapper classes and the boxed objects are automatically unboxed to get the primitive values. But that’s not the only place you can see autoboxing and unboxing.

  • You can see it in methods.
  • You can see it in expressions.
  • Due to Autoboxing and unboxing a wrapper class can be used in a switch statement.

Autoboxing in method parameters

When an argument is passed to a method or a value is returned from a method autoboxing/unboxing can happen, if required.

Example code

Here the method calcValue takes Double object as a parameter but primitive value double is passed while calling the method thus the double value is autoboxed to a Double type object.

Then there is an expression also which has a mix of Double object and double value -

dOb = dNum + 13.4;

And it is again assigned to a Double type.

Again the return type of the method is double and the returned value is assigned to a Double object.

public class BoxDemo {

 public static void main(String[] args) {
  BoxDemo bd = new BoxDemo();
  
  Double dVal = bd.calcValue(15.6);
  System.out.println("Value " + dVal);

 }
 
 public double calcValue(Double dNum){
  Double dOb;
  dOb = dNum + 13.4;
  return dOb;
 }
}

Use in switch statement

Due to unboxing Integer object can be used in a switch statement too. With in the switch statement object is unboxed and the int value is used for the conditions.

Integer iVal = 7;
switch(iVal) {
 case 1: System.out.println("First step");
 break;
 case 2: System.out.println("Second step");
 break;
 default: System.out.println("There is some error");
}

AutoBoxing and Unboxing overhead

There is some overhead involved in the process of autoboxing/unboxing so make sure you don’t start using objects exclusively thinking autoboxing/unboxing will happen anyway behind the scenes. It will happen but don’t forget the involved overhead thus making the code a little less efficient.

That's all for this topic AutoBoxing and UnBoxing in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Enum Type in Java
  2. Comparing Enum to String - Java Program
  3. covariant return type in Java
  4. Wildcard in Java Generics
  5. varargs in Java

You may also like -

>>>Go to Java advance topics page

No comments:

Post a Comment