Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Autoboxing
Java Arrays Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Arrays Micro Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of understanding regarding Java arrays.
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
"Autoboxing" is a way to work with primitive data types within ArrayLists to make your coding easier. If we wanted to create an ArrayList of integers, we use ArrayList<Integer> because int is not an object. However, when we call methods such as add() and set(), instead of creating a new object add(new Integer(11));, we can just write add(11) as Java automatically converts int into Integer for us. Look below for an example of autoboxing.
AutoBoxingExample.java
package exlcode;
import java.util.ArrayList;
public class AutoBoxingExample {
public static ArrayList exampleVariableOne = new ArrayList();
public static void main(String[] args) {
exampleVariableOne.add(new Integer(5));
// autoboxing leads to simple code without unnecessary boxing
exampleVariableOne.add(80);
System.out.println(exampleVariableOne);
}
}
Autoboxing automatically converts int values to type Integer when the program is run. Therefore, regardless of what primitive data type we want to use, as long as we initialize the ArrayList with the right object, we can add primitive data types without creating new objects.
Use autoboxing when creating an ArrayList of objects that have corresponding primitive data types. It makes coding a lot more simple when debugging the code.