[!NOTE] We briefly touched on Wrapper classes when discussing
ArrayLists. Because Java Generics (<Integer>) can physically only handle Objects living on the Heap memory, they cannot process pure primitives likeintordouble.
Wrapper classes wrap the raw primitive data inside a full Object shell, granting them access to hundreds of methods.
The Wrapper Conversions
| Primitive Data Type | Wrapper Class |
|---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
boolean |
Boolean |
char |
Character |
Creating Wrapper Objects
You can instantiate Wrapper classes just like any other object.
Integer myInt = 5; // Automatically wraps it!
Double myDouble = 5.99;
Character myChar = 'A';
// Because they are fully-fledged Objects, you can call Methods on them!
System.out.println(myInt.intValue()); // Extracts the raw primitive back out.
System.out.println(myDouble.doubleValue());
// Converting an Integer object directly into a String representation!
String myString = myInt.toString();
System.out.println(myString.length()); // Outputs 1
Autoboxing and Unboxing
Historically, developers had to manually write Integer myInt = new Integer(5); and then invoke myInt.intValue() to extract the 5 out every time they did math.
To reduce boilerplate, modern Java compilers employ Autoboxing and Unboxing.
- Autoboxing: The automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an
intdirectly to anInteger. - Unboxing: The aggressive automatic conversion of wrapper class to primitive.
// Autoboxing:
// Java sees primitive '10', creates an 'Integer' object in Heap memory silently, and adds it!
ArrayList<Integer> li = new ArrayList<>();
li.add(10);
// Unboxing:
// Java retrieves the 'Integer' object, quietly extracts the intValue(), and does the raw math!
int result = li.get(0) + 15;
[!CAUTION] Avoid Autoboxing in massive
forloop mathematical calculations. Constructing and destroying 10,000,000Integerobjects in a tight loop is extremely CPU intensive compared to using rawintprimitives.
Wrappers Also Represent Missing Values
Primitive values such as int cannot be null. Wrapper objects such as Integer can be null. This is useful when a value may be unknown, but it also creates a common unboxing danger.
Unboxing Null Can Crash
Integer score = null;
// int rawScore = score; // NullPointerException during unboxing
Be careful when unboxing wrapper values that may be null, especially values coming from databases, maps, or optional input.
Parsing Strings
String input = "42";
int value = Integer.parseInt(input);
String decimalInput = "19.5";
double amount = Double.parseDouble(decimalInput);
Common Mistakes
- Using wrappers in tight numeric loops when primitives would be faster.
- Unboxing a null wrapper value.
- Comparing wrapper objects with
==instead of comparing values carefully. - Forgetting that collections require wrapper types, not primitives.
Mini Practice
Parse a list of string marks into integers. Skip invalid values safely. Then calculate the average using primitives after parsing.
Practice Lab: Parse and Average Marks
Use wrapper utilities to convert text into numbers safely.
- Create a string array:
{"80", "76", "bad", "91"}. - Loop through each value.
- Use
Integer.parseIntinside try-catch. - Skip invalid values.
- Calculate the average of valid marks only.
Goal: Practice wrapper parsing, invalid input handling, and primitive arithmetic after parsing.
Revision Checkpoint
- Wrapper: Object version of a primitive, such as
Integerforint. - Autoboxing: Primitive to wrapper conversion.
- Unboxing: Wrapper to primitive conversion.
- Collections: Require wrapper types, not primitives.
- Null warning: Unboxing a null wrapper can crash.
Before the quiz: Explain why ArrayList<int> is invalid but ArrayList<Integer> works.