Wrapper classes let us use simple values like numbers and letters as objects. This helps when we need to store or work with these values in special ways.
When you want to store numbers or characters in collections like ArrayList that only accept objects.
When you need to convert between strings and numbers or characters.
When you want to use utility methods that come with wrapper classes, like checking if a string is a valid number.
When you need to treat primitive values as objects to use features like synchronization or generics.
Syntax
Java
WrapperClassName variableName = new WrapperClassName(primitiveValue);
Wrapper classes have names like Integer, Double, Character, Boolean.
Since Java 5, autoboxing lets you assign primitives directly to wrapper objects without 'new'.
Examples
Creates an Integer object holding the value 10.
Java
Integer num = newInteger(10);
Automatically converts the primitive double 19.99 to a Double object.
Java
Double price = 19.99; // autoboxing
Wraps the char 'A' into a Character object.
Java
Character letter = 'A';
Sample Program
This program shows how wrapper classes let us store numbers in an ArrayList and convert strings to numbers easily.
Java
import java.util.ArrayList;
publicclass WrapperExample {
publicstaticvoid main(String[] args) {
ArrayList<Integer> numbers = newArrayList<>();
// Add primitive int values, autoboxed to Integer objects
numbers.add(5);
numbers.add(10);
numbers.add(15);
// Print all numbersfor (Integer num : numbers) {
System.out.println(num);
}
// Convert String to IntegerString str = "123";
int value = Integer.parseInt(str);
System.out.println("Parsed value: " + value);
}
}
OutputSuccess
Important Notes
Wrapper classes are immutable, meaning their value cannot change once created.
Autoboxing and unboxing make using wrapper classes easier by converting automatically between primitives and objects.
Summary
Wrapper classes let primitive values be used as objects.
They are needed for collections and utility methods.
Autoboxing simplifies working with wrapper classes.
Practice
(1/5)
1. Why are wrapper classes used in Java?
easy
A. To replace all primitive types with objects
B. To improve the speed of primitive operations
C. To allow primitive types to be treated as objects
D. To create new primitive types
Solution
Step 1: Understand primitive vs object types
Primitive types like int or double are not objects, so they can't be used where objects are required.
Step 2: Role of wrapper classes
Wrapper classes like Integer or Double wrap primitive values so they can be used as objects.
Final Answer:
To allow primitive types to be treated as objects -> Option C
Quick Check:
Wrapper classes = primitive as objects [OK]
Hint: Wrapper classes wrap primitives as objects [OK]
Common Mistakes:
Thinking wrapper classes speed up primitive operations
Confusing wrapper classes with new primitive types
2. Which of the following is the correct way to create an Integer wrapper object from an int value 10?
easy
A. Integer obj = new Integer(10);
B. int obj = Integer(10);
C. Integer obj = new int(10);
D. Integer obj = Integer.value(10);
Solution
Step 1: Check syntax for creating Integer object
The constructor Integer(int) creates an Integer object from an int value.
Step 2: Validate each option
Integer obj = new Integer(10); uses new Integer(10), which is correct syntax. int obj = Integer(10); is invalid syntax. Integer obj = new int(10); is invalid as primitive int has no constructor. Integer obj = Integer.value(10); uses a non-existent method value().
Final Answer:
Integer obj = new Integer(10); -> Option A
Quick Check:
Use new Integer(int) to create wrapper [OK]
Hint: Use new Integer(value) to create wrapper object [OK]
Common Mistakes:
Using invalid method names like Integer.value()
Trying to instantiate primitives like new int(10)
Confusing primitive and wrapper syntax
3. What will be the output of the following code?
List<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
System.out.println(list.get(0) + list.get(1));
medium
A. 510
B. 15
C. Compilation error
D. Runtime error
Solution
Step 1: Understand autoboxing in collections
List<Integer> stores Integer objects. Adding int values 5 and 10 autoboxes them to Integer objects.
Step 2: Evaluate the addition operation
list.get(0) and list.get(1) return Integer objects, which unbox to int when added, so 5 + 10 = 15.
Final Answer:
15 -> Option B
Quick Check:
Autoboxing/unboxing sums to 15 [OK]
Hint: Autoboxing lets you add Integer values like ints [OK]
Common Mistakes:
Thinking list.get returns int directly
Expecting string concatenation instead of addition
Assuming compilation or runtime errors
4. Identify the error in the following code snippet:
int a = 5;
Integer b = null;
int c = a + b;
medium
A. No error, code runs fine
B. Compilation error due to incompatible types
C. ArithmeticException at runtime
D. NullPointerException at runtime
Solution
Step 1: Analyze autounboxing of Integer b
Variable b is an Integer object set to null. When used in arithmetic, it is unboxed to int.
Step 2: Understand null unboxing behavior
Unboxing null causes NullPointerException at runtime because there is no int value to extract.
Expecting compilation error instead of runtime error
Assuming code runs without error
Confusing NullPointerException with ArithmeticException
5. You want to store primitive int values in a collection and perform arithmetic operations easily. Which approach best uses wrapper classes to achieve this?
hard
A. Use List<Integer> and rely on autoboxing/unboxing
B. Use List<int> since it stores primitives directly
C. Convert int to String before adding to collection
D. Use arrays only, wrapper classes are not needed
Solution
Step 1: Understand collections and primitives
Java collections cannot store primitive types like int directly; they require objects.
Step 2: Use wrapper classes with autoboxing
List<Integer> stores Integer objects. Autoboxing converts int to Integer automatically, allowing easy arithmetic with unboxing.
Final Answer:
Use List<Integer> and rely on autoboxing/unboxing -> Option A
Quick Check:
Collections need wrapper classes for primitives [OK]
Hint: Use List<Integer> for primitives with autoboxing [OK]
Common Mistakes:
Trying to use List<int> which is invalid
Storing primitives as Strings unnecessarily
Avoiding wrapper classes in collections
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.