📖 Scenario: Imagine you are managing a small store's inventory system. You need to work with product quantities, which are numbers. Sometimes you use simple numbers, and sometimes you need to use objects that hold numbers. Java helps by automatically converting between these two forms, called autoboxing.
🎯 Goal: You will create a Java program that shows how autoboxing works by converting between primitive int and the wrapper class Integer. You will store numbers in an ArrayList of Integer and perform simple operations.
📋 What You'll Learn
Create an ArrayList of Integer to hold product quantities.
Add primitive int values to the ArrayList using autoboxing.
Retrieve values from the ArrayList and use them as primitive int using unboxing.
Calculate the total quantity of all products.
Print the total quantity.
💡 Why This Matters
🌍 Real World
Autoboxing helps when working with collections like <code>ArrayList</code> that cannot hold primitive types directly. It makes code simpler and cleaner by automatically converting between primitives and objects.
💼 Career
Understanding autoboxing is important for Java developers because it is used frequently in everyday coding, especially when handling data collections, APIs, and frameworks that require objects instead of primitives.
Progress0 / 4 steps
1
Create an ArrayList of Integer
Create an ArrayList called quantities that can hold Integer objects.
Java
Hint
Use ArrayList<Integer> and the new keyword to create the list.
2
Add primitive int values to the ArrayList
Add the primitive int values 10, 20, and 30 to the quantitiesArrayList. Java will autobox them to Integer objects.
Java
Hint
Use the add method to add each number directly. Java will convert the int to Integer automatically.
3
Calculate the total quantity using unboxing
Create an int variable called total and set it to 0. Use a for loop with variable qty to iterate over quantities. Add each qty to total. This uses unboxing to convert Integer to int automatically.
Java
Hint
Use a for-each loop to go through quantities. Add each qty to total.
4
Print the total quantity
Write a System.out.println statement to print the text Total quantity: followed by the value of total.
Java
Hint
Use System.out.println with a string and the total variable to show the result.
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
Step 1: Understand primitive and wrapper types
Java has primitive types like int and wrapper classes like Integer.
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 A
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
Step 1: Check each option for autoboxing usage
Autoboxing converts primitive to wrapper automatically, so Integer 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 A
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
Step 1: Understand autounboxing in expression
The Integer a is autounboxed to int to add 10.
Step 2: Calculate the sum
5 + 10 = 15, so b is 15 and printed.
Final Answer:
15 -> Option C
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
Step 1: Analyze autounboxing with null wrapper
Variable x is null, so autounboxing to int causes error.
Step 2: Identify runtime exception
Autounboxing null throws NullPointerException at runtime.
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
Step 1: Understand autoboxing in list addition
Primitive int i is autoboxed to Integer when added to the list.
Step 2: Understand autounboxing in sum calculation
Each Integer in list is autounboxed to int for addition to sum.
Step 3: Calculate the sum
Sum of 1 + 2 + 3 = 6.
Final Answer:
6 -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.