Bird
Raised Fist0
Javaprogramming~10 mins

Autoboxing in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Autoboxing
Primitive value
↓
Autoboxing: Convert to Wrapper Object
↓
Use Wrapper Object as needed
↓
Unboxing: Convert back to Primitive (if needed)
↓
Primitive value ready for operations
Autoboxing automatically converts primitive types to their wrapper objects when needed, and unboxing converts them back.
Execution Sample
Java
int num = 5;
Integer boxedNum = num; // autoboxing
int unboxedNum = boxedNum; // unboxing
System.out.println(boxedNum);
System.out.println(unboxedNum);
This code shows autoboxing of int to Integer and unboxing back to int, then prints both.
Execution Table
StepActionVariableValueExplanation
1Declare primitive intnum5num holds primitive int 5
2Autoboxing: assign int to IntegerboxedNumInteger(5)num is converted to Integer object automatically
3Unboxing: assign Integer to intunboxedNum5boxedNum is converted back to primitive int
4Print boxedNumOutput5Integer object's value printed as 5
5Print unboxedNumOutput5Primitive int printed as 5
6End--Program ends
💡 All steps complete, program ends after printing values.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
numundefined5555
boxedNumundefinedundefinedInteger(5)Integer(5)Integer(5)
unboxedNumundefinedundefinedundefined55
Key Moments - 3 Insights
Why can we assign a primitive int directly to an Integer object without explicit conversion?
Because autoboxing automatically converts the primitive int to an Integer object as shown in step 2 of the execution_table.
What happens when we assign an Integer object to a primitive int variable?
Unboxing automatically converts the Integer object back to a primitive int, as shown in step 3 of the execution_table.
Does printing an Integer object print the object reference or the primitive value?
Printing an Integer object prints its primitive value, as shown in step 4 where boxedNum prints 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the value of boxedNum after autoboxing?
APrimitive int 5
Bnull
CInteger object holding 5
DString "5"
💡 Hint
Check the 'Value' column for boxedNum at step 2 in execution_table.
At which step does unboxing occur in the execution_table?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for when Integer is converted back to int in the 'Action' column.
If we remove autoboxing and try to assign int to Integer without conversion, what happens?
ACompilation error
BRuns fine with autoboxing
CRuntime error
DInteger becomes null
💡 Hint
Autoboxing allows automatic conversion; without it, assignment causes compile error.
Concept Snapshot
Autoboxing in Java:
- Automatically converts primitives (int, double, etc.) to wrapper objects (Integer, Double).
- Happens when assigning primitive to wrapper type.
- Unboxing converts wrapper back to primitive.
- Simplifies code by removing manual conversion.
- Example: Integer i = 5; int n = i;
Full Transcript
Autoboxing is a Java feature that automatically converts primitive types like int into their wrapper classes like Integer when needed. This means you can assign a primitive int directly to an Integer variable without writing extra code. Similarly, unboxing converts the wrapper object back to a primitive type automatically. In the example, we start with an int variable num set to 5. When we assign num to boxedNum of type Integer, autoboxing converts the int 5 into an Integer object holding 5. Later, when we assign boxedNum back to unboxedNum of type int, unboxing converts the Integer object back to the primitive int 5. Printing both variables shows the value 5. This feature makes code simpler and easier to read by handling conversions behind the scenes.

Practice

(1/5)
1. What is autoboxing in Java?
easy
A. Automatic conversion of primitive types to their wrapper class objects
B. Manual conversion of objects to primitive types
C. A way to box arrays into collections
D. Conversion of strings to numbers

Solution

  1. Step 1: Understand primitive and wrapper types

    Java has primitive types like int and wrapper classes like Integer.
  2. Step 2: Define autoboxing

    Autoboxing means Java automatically converts a primitive type to its wrapper object when needed.
  3. Final Answer:

    Automatic conversion of primitive types to their wrapper class objects -> Option A
  4. Quick Check:

    Autoboxing = automatic primitive to object conversion [OK]
Hint: Autoboxing means automatic primitive to object conversion [OK]
Common Mistakes:
  • Confusing autoboxing with manual conversion
  • Thinking autoboxing converts objects to primitives
  • Mixing autoboxing with string parsing
2. Which of the following code snippets correctly uses autoboxing?
easy
A. Integer num = 10;
B. int num = new Integer(10);
C. int num = Integer.parseInt("10");
D. int num = Integer.valueOf(10);

Solution

  1. Step 1: Check each option for autoboxing usage

    Autoboxing converts primitive to wrapper automatically, so Integer num = 10; is valid.
  2. 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.
  3. Final Answer:

    Integer num = 10; -> Option A
  4. Quick Check:

    Assigning primitive to wrapper uses autoboxing [OK]
Hint: Assign primitive to wrapper for autoboxing [OK]
Common Mistakes:
  • Using new Integer() instead of autoboxing
  • Assigning wrapper object directly to primitive without unboxing
  • Confusing parsing methods with autoboxing
3. What is the output of the following code?
Integer a = 5;
int b = a + 10;
System.out.println(b);
medium
A. NullPointerException
B. Compilation error
C. 15
D. 5

Solution

  1. Step 1: Understand autounboxing in expression

    The Integer a is autounboxed to int to add 10.
  2. Step 2: Calculate the sum

    5 + 10 = 15, so b is 15 and printed.
  3. Final Answer:

    15 -> Option C
  4. Quick Check:

    Autounboxing + addition = 15 [OK]
Hint: Wrapper auto-unboxes in arithmetic expressions [OK]
Common Mistakes:
  • Expecting compilation error due to wrapper usage
  • Confusing autoboxing with autounboxing
  • Thinking NullPointerException occurs without null
4. Identify the error in the following code snippet:
Integer x = null;
int y = x + 5;
System.out.println(y);
medium
A. Compilation error due to null assignment
B. Output is 0
C. Output is 5
D. NullPointerException at runtime

Solution

  1. Step 1: Analyze autounboxing with null wrapper

    Variable x is null, so autounboxing to int causes error.
  2. Step 2: Identify runtime exception

    Autounboxing null throws NullPointerException at runtime.
  3. Final Answer:

    NullPointerException at runtime -> Option D
  4. Quick Check:

    Autounboxing null causes NullPointerException [OK]
Hint: Autounboxing null causes NullPointerException [OK]
Common Mistakes:
  • Expecting compilation error instead of runtime error
  • Assuming null converts to zero
  • Ignoring autounboxing behavior
5. Given the code below, what will be the output?
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);
hard
A. Compilation error due to autoboxing
B. 6
C. NullPointerException
D. 0

Solution

  1. Step 1: Understand autoboxing in list addition

    Primitive int i is autoboxed to Integer when added to the list.
  2. Step 2: Understand autounboxing in sum calculation

    Each Integer in list is autounboxed to int for addition to sum.
  3. Step 3: Calculate the sum

    Sum of 1 + 2 + 3 = 6.
  4. Final Answer:

    6 -> Option B
  5. Quick Check:

    Autoboxing + autounboxing sum = 6 [OK]
Hint: Autoboxing in collections, autounboxing in arithmetic sum [OK]
Common Mistakes:
  • Expecting errors with autoboxing in collections
  • Forgetting autounboxing in sum calculation
  • Assuming NullPointerException without null values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard