Unboxing in Java - Time & Space Complexity
We want to understand how unboxing affects the time it takes for a program to run.
Specifically, we ask: how does unboxing operations grow as the input size changes?
Analyze the time complexity of the following code snippet.
Integer[] numbers = new Integer[n];
int sum = 0;
for (int i = 0; i < n; i++) {
sum += numbers[i]; // unboxing happens here
}
System.out.println(sum);
This code sums up values stored as Integer objects by unboxing them to int.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Unboxing each Integer to int inside the loop.
- How many times: Exactly once for each element, so n times.
Each element requires one unboxing operation, so as the number of elements grows, the total work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 unboxing operations |
| 100 | 100 unboxing operations |
| 1000 | 1000 unboxing operations |
Pattern observation: The work grows in a straight line with input size.
Time Complexity: O(n)
This means the time to complete the sum grows directly in proportion to the number of elements.
[X] Wrong: "Unboxing is free and does not affect performance."
[OK] Correct: Each unboxing is a small operation that happens every time you convert from Integer to int, so it adds up when done many times.
Understanding how unboxing affects time helps you write efficient code and explain performance in real projects.
"What if we replaced the Integer array with an int array? How would the time complexity change?"
Practice
unboxing mean in Java?Solution
Step 1: Understand wrapper and primitive types
Wrapper classes like Integer wrap primitive types like int.Step 2: Define unboxing
Unboxing is the automatic conversion from wrapper object to primitive type.Final Answer:
Automatically converting a wrapper object to its primitive type -> Option BQuick Check:
Unboxing = wrapper to primitive [OK]
- Confusing unboxing with boxing (primitive to wrapper)
- Thinking unboxing converts to String
- Assuming unboxing requires manual code
Solution
Step 1: Check each option for correct syntax and unboxing
Integer num = 10; int val = num; assigns Integer object to int variable, triggering unboxing automatically.Step 2: Identify errors in other options
Integer val = new Integer(10); assigns wrapper object to wrapper variable (no unboxing); Integer num = int 10; has invalid syntax; Integer val = Integer.valueOf(10); assigns wrapper object to wrapper variable (no unboxing).Final Answer:
Integer num = 10; int val = num; -> Option AQuick Check:
Wrapper to primitive assignment triggers unboxing [OK]
- Assigning wrapper constructors directly to wrapper variables (no unboxing)
- Incorrect syntax like 'Integer num = int 10;'
- Confusing boxing and unboxing syntax
Integer a = 5; int b = a + 10; System.out.println(b);
Solution
Step 1: Understand unboxing in expression
Variable 'a' is Integer object; adding 10 triggers unboxing 'a' to int 5.Step 2: Calculate the sum and print
5 + 10 = 15, so output is 15 printed.Final Answer:
15 -> Option DQuick Check:
Unboxing Integer 5 + 10 = 15 [OK]
- Thinking it concatenates as string '510'
- Expecting compilation error due to wrapper usage
- Confusing with NullPointerException if variable was null
Integer num = null; int val = num; System.out.println(val);
Solution
Step 1: Analyze unboxing of null Integer
Variable 'num' is null; unboxing null to int causes runtime error.Step 2: Identify exception thrown
Unboxing null throws NullPointerException at runtime.Final Answer:
Runtime NullPointerException during unboxing -> Option CQuick Check:
Unboxing null causes NullPointerException [OK]
- Expecting compilation error instead of runtime error
- Assuming null unboxes to zero
- Thinking it prints 'null' string
List<Integer> list = List.of(1, 2, null, 4);
int sum = 0;
for (Integer n : list) {
sum += n; // unboxing happens here
}
System.out.println(sum);Solution
Step 1: Understand unboxing in loop with null element
List contains a null Integer; unboxing null causes error.Step 2: Identify runtime behavior
When loop reaches null, unboxing triggers NullPointerException.Final Answer:
Runtime NullPointerException at unboxing null -> Option AQuick Check:
Unboxing null in loop causes runtime exception [OK]
- Assuming null is skipped or treated as zero
- Expecting compilation error due to null in list
- Thinking sum is partial ignoring null
Ready to prove it?
Pass the test and this pattern moves unknown
