Autoboxing lets Java automatically convert simple values like numbers into objects, so you don't have to do it yourself.
Autoboxing in Java
Integer obj = 10; // int 10 is autoboxed to Integer object Double dObj = 3.14; // double 3.14 is autoboxed to Double object
Autoboxing happens automatically when assigning a primitive value to its wrapper class.
Wrapper classes include Integer, Double, Boolean, etc., which are object versions of primitives.
Integer num = 5; // int 5 autoboxed to Integer
Double price = 9.99; // double 9.99 autoboxed to Double
Boolean flag = true; // boolean true autoboxed to Boolean
This program shows autoboxing by adding int values directly to an ArrayList of Integer objects. Java converts the ints to Integer objects automatically.
import java.util.ArrayList; public class AutoboxingExample { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); // Autoboxing: int 10 is automatically converted to Integer object numbers.add(10); numbers.add(20); numbers.add(30); // Print all numbers for (Integer num : numbers) { System.out.println(num); } } }
Autoboxing makes code simpler but can add small performance overhead because of object creation.
Be careful when comparing wrapper objects with ==; use .equals() to compare values.
Autoboxing automatically converts primitive values to their wrapper objects.
It helps when working with collections or APIs that require objects.
Use autoboxing for cleaner and easier code without manual conversions.
Practice
autoboxing in Java?Solution
Step 1: Understand primitive and wrapper types
Java has primitive types likeintand wrapper classes likeInteger.Step 2: Define autoboxing
Autoboxing means Java automatically converts a primitive type to its wrapper object when needed.Final Answer:
Automatic conversion of primitive types to their wrapper class objects -> Option AQuick Check:
Autoboxing = automatic primitive to object conversion [OK]
- Confusing autoboxing with manual conversion
- Thinking autoboxing converts objects to primitives
- Mixing autoboxing with string parsing
Solution
Step 1: Check each option for autoboxing usage
Autoboxing converts primitive to wrapper automatically, soInteger num = 10;is valid.Step 2: Identify invalid autoboxing
int num = Integer.parseInt("10");uses string parsing to primitive (no autoboxing).int num = new Integer(10);uses manual construction followed by unboxing.int num = Integer.valueOf(10);passes primitive to method (internal boxing) then unboxes.Final Answer:
Integer num = 10; -> Option AQuick Check:
Assigning primitive to wrapper uses autoboxing [OK]
- Using new Integer() instead of autoboxing
- Assigning wrapper object directly to primitive without unboxing
- Confusing parsing methods with autoboxing
Integer a = 5; int b = a + 10; System.out.println(b);
Solution
Step 1: Understand autounboxing in expression
TheInteger ais autounboxed tointto add 10.Step 2: Calculate the sum
5 + 10 = 15, sobis 15 and printed.Final Answer:
15 -> Option CQuick Check:
Autounboxing + addition = 15 [OK]
- Expecting compilation error due to wrapper usage
- Confusing autoboxing with autounboxing
- Thinking NullPointerException occurs without null
Integer x = null; int y = x + 5; System.out.println(y);
Solution
Step 1: Analyze autounboxing with null wrapper
Variablexisnull, so autounboxing tointcauses error.Step 2: Identify runtime exception
Autounboxing null throwsNullPointerExceptionat runtime.Final Answer:
NullPointerException at runtime -> Option DQuick Check:
Autounboxing null causes NullPointerException [OK]
- Expecting compilation error instead of runtime error
- Assuming null converts to zero
- Ignoring autounboxing behavior
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
list.add(i); // autoboxing here
}
int sum = 0;
for (Integer num : list) {
sum += num; // autounboxing here
}
System.out.println(sum);Solution
Step 1: Understand autoboxing in list addition
Primitiveint iis autoboxed toIntegerwhen added to the list.Step 2: Understand autounboxing in sum calculation
EachIntegerin list is autounboxed tointfor addition tosum.Step 3: Calculate the sum
Sum of 1 + 2 + 3 = 6.Final Answer:
6 -> Option BQuick Check:
Autoboxing + autounboxing sum = 6 [OK]
- Expecting errors with autoboxing in collections
- Forgetting autounboxing in sum calculation
- Assuming NullPointerException without null values
Ready to prove it?
Pass the test and this pattern moves unknown
