📖 Scenario: Imagine you have a box that holds a number, but the number is wrapped inside an object. You want to take the number out of the box to use it as a simple number in your program.
🎯 Goal: You will learn how to convert a wrapped number (an Integer object) into a simple number (int) using unboxing in Java.
📋 What You'll Learn
Create an Integer object with a specific value
Create an int variable to hold the unboxed value
Use unboxing to convert the Integer object to int
Print the unboxed int value
💡 Why This Matters
🌍 Real World
Unboxing is useful when you receive numbers as objects but need to use them as simple numbers for calculations or comparisons.
💼 Career
Understanding unboxing helps in writing efficient Java code, especially when working with collections or APIs that use wrapper classes.
Progress0 / 4 steps
1
Create an Integer object
Create an Integer object called boxedNumber and set it to 100.
Java
Hint
Use Integer boxedNumber = 100; to create the object.
2
Create an int variable for unboxing
Create an int variable called unboxedNumber to hold the unboxed value from boxedNumber.
Java
Hint
Assign boxedNumber directly to unboxedNumber to unbox.
3
Use unboxed value in a calculation
Add 50 to unboxedNumber and store the result in a new int variable called result.
Java
Hint
Use int result = unboxedNumber + 50; to add 50.
4
Print the result
Print the value of result using System.out.println.
Java
Hint
Use System.out.println(result); to display the number.
Practice
(1/5)
1. What does unboxing mean in Java?
easy
A. Converting a primitive type to a String
B. Automatically converting a wrapper object to its primitive type
C. Manually converting a primitive type to a wrapper object
D. Automatically converting a String to a wrapper object
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 B
Quick Check:
Unboxing = wrapper to primitive [OK]
Hint: Unboxing means wrapper to primitive automatically [OK]
Common Mistakes:
Confusing unboxing with boxing (primitive to wrapper)
Thinking unboxing converts to String
Assuming unboxing requires manual code
2. Which of the following Java statements correctly uses unboxing?
easy
A. Integer num = 10; int val = num;
B. Integer val = new Integer(10);
C. Integer num = int 10;
D. Integer val = Integer.valueOf(10);
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 A
Quick Check:
Wrapper to primitive assignment triggers unboxing [OK]
Hint: Assign wrapper object to primitive variable to unbox [OK]
Common Mistakes:
Assigning wrapper constructors directly to wrapper variables (no unboxing)
Incorrect syntax like 'Integer num = int 10;'
Confusing boxing and unboxing syntax
3. What is the output of this Java code?
Integer a = 5;
int b = a + 10;
System.out.println(b);
medium
A. NullPointerException
B. 510
C. Compilation error
D. 15
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 D
Quick Check:
Unboxing Integer 5 + 10 = 15 [OK]
Hint: Unboxing happens automatically in arithmetic expressions [OK]
Common Mistakes:
Thinking it concatenates as string '510'
Expecting compilation error due to wrapper usage
Confusing with NullPointerException if variable was null
4. Identify the error in this code snippet:
Integer num = null;
int val = num;
System.out.println(val);
medium
A. Prints 0 without error
B. Compilation error due to null assignment
C. Runtime NullPointerException during unboxing
D. Prints 'null' as output
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 C