Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Autoboxing and Unboxing



Java Autoboxing

Autoboxing is a technique used by Java compiler to automatically convert a primitive value to its wrapper counterpart. For example, when an int value is assigned to an Integer wrapper object, compiler automatically converts the int value to the object without any need to explicitly cast the int value or by calling any method to convert int to Integer object. Autoboxing is also known as boxing.

// Autoboxing
Integer obj = 10;

// Explicit casting
Integer obj2 = Integer.valueOf(10)

In both cases, the wrapper objects are initialized with an int value. In first case, autoboxing is playing role and second case, we're explicitly casting int value to Integer wrapper object.

Compiler uses autoboxing in following scenarios −

  • If a primitive value is passed as an argument to a function which is expecting a wrapper class object.

  • If a primitive value is assigned to a variable of the type of wrapper class.

Example of Autoboxing in Java

In this example, we've created a list of Integer as List can contain only objects. Now while adding the item to this list, we're not creating any Integer object but we're just passing the primitive int values. Java compiler automatically handles the conversion and program compiles successfully. We've used another case of assigning a char primitive value to Character object which works as well.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class Tester {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>();

      for(int i = 0; i< 10; i++){
         // autoboxing by passing as an argument
         // int value is converted to Integer
         // by compiler during compilation
         list.add(i);
      }

      System.out.println(list);

      char c = 'a';          
      //autoboxing by assigning a char to Character object
      Character ch = c;
      System.out.println(ch);
   }
}

Output

Let us compile and run the above program without any command line argument, this will produce the following result −

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a

Java Unboxing

Unboxing is reverse of autoboxing. Unboxing is used by Java compiler to convert a wrapper object to its primitive counterpart. For example when an Integer object is passed to a method as argument but the method called is expecting an int variable, then compiler automatically converts the Integer object to int value and then pass it to the mathod called. Similarly Java compilier unboxes the wrapper value if it is assigned to a primitive variable. Thus we are not required to explicitly get the int value from the wrapper object.

Integer obj = Integer.valueOf(10);

// Unboxing
int i = obj;

// Explicit value deduction
i = obj.intValue();

In both cases, the primitive values are initialized with an int value. In first case, unboxing is playing role and second case, we're explicitly getting int value from an Integer wrapper object.

Compiler uses unboxing in following scenarios −

  • If a wrapper class object is passed as an argument to a function which is expecting a primitive value.

  • If a wrapper class object is assigned to a variable of primitive type.

Example of Unboxing in Java

In this example, we've created an Integer object and initilized it with a value of 10. This object is passed to abs() method which is expecting an int, a primitive variable. Java compiler automatically handles the conversion and program compiles successfully. We've used another case of assigning a Integer object to int variable which works as well.

package com.tutorialspoint;

public class Tester {
   public static void main(String[] args) {
      Integer integer = Integer.valueOf(-10);
      // unboxing by passing as an argument
      // Integer object is converted to int
      // by compiler during compilation
      int i = abs(integer);        
      System.out.println(i);

      //unboxing by assigning an Integer object to int variable
      int j = integer;        
      System.out.println(j);
   }
 
   private static int abs(int i){
      return (i < 0)? -i: i;
   }
}

Output

Let us compile and run the above program without any command line argument, this will produce the following result −

10
-10

Mapping of Primitive and Wrapper Objects

Sr. No.PrimitiveWrappermethod to get value
1byteBytebyteValue()
2shortShortshortValue()
3intIntegerintValue()
4longLonglongValue()
5floatFloatfloatValue()
6doubleDoubledoubleValue()
7charCharactercharValue()
8booleanBooleanbooleanValue()
Advertisements