java

Autoboxing and Autounboxing in Java

Autoboxing is the automatic conversion of a primitive data type into its wrapper class. Autounboxing refers to the automatic conversion of a wrapper class into the corresponding primitive datatype. This language feature was introduced with Java version 1.5.

Auto(un)boxing is quickly explained and it’s worthy to dedicate for it a separate chapter. A small example shows you the possibilities with Auto(un)boxing:

public class Main {
	
  public static void main(String args[]) throws Exception {

    Main app = new Main();
	  
    int i = 5;
    Integer ig = new Integer(10);

    app.displayAnInt(i);
    app.displayAnInt(ig); // autounboxing
	  
    app.displayAnInteger(i); // autoboxing
    app.displayAnInteger(ig);
  }
  
  public void displayAnInt(int i) {
    System.out.println("This is an int: "+ i);
  }
  
  public void displayAnInteger(Integer i) {
    System.out.println("This is an Integer: "+ i);
  }
}

Output:

This is an int: 5
This is an int: 10
This is an Integer: 5
This is an Integer: 10
 
If a primitive data type is expected but a wrapper class is passed, the wrapper class is automatically converted to a primitive data type. The same applies vice versa, of course. If an application expects a wrapper class but is called with a primitive data type, the primitive data type is automatically converted to its wrapper class.

However, there are some issues that need to be considered when working with Auto(un)boxing. For example, the conversion is only done if it’s absolutely necessary.
Anonymous Classes in JavaAnonymous Classes in JavaAnonymous Classes (or Anonymous Inner Classes) are nameless classes, more precisely, a class with no name that’s defined and initialized at the same time. Anonymous…Read More

Autoboxing

A simple example of autoboxing:

int i = 1;
Integer ig = i;

But why is it necessary to use integers, if int also works? One reason are generic classes like List or Map. These cannot be created with simple data types but only with an object-oriented type, i.e. a class:

List<int> liste = new ArrayList<int>();  // does not compile

Attempting to create a list with a simple data type will fail during compilation. So here the object-oriented type is necessary:

List<Integer> liste = new ArrayList<Integer>();
int i = 1;
liste.add(i); // works thanks to autoboxing

Autoboxing is used when a simple value is passed to a method that expects the object-oriented type, or when a simple value is assigned to a variable of the object-oriented type.
 

 

Autounboxing

Autounboxing is exactly the opposite of autoboxing: converting the object of a class into a simple data type.

List<Integer> liste = new ArrayList<>();
liste.add(1);     //autoboxing
liste.add(2);     //autoboxing
liste.add(3);     //autoboxing

for(int i:liste){ // unboxing Integer -> int
    System.out.println(i);
}

Autounboxing is used when an object is passed to a method that expects the simple type, or when an object is assigned to a variable of the simple type.
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *