29. Wrapper Classes & Autoboxing | Java Course - Study Chapter | QuizMaker

Bridging the gap between Primitives and true Objects.

Read
14m
Type
Chapter
Access
Free

Course

Java Programming: From Zero to Enterprise

Topic

3. Core APIs & I/O

[!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 like int or double.

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: 
// 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 for loop mathematical calculations. Constructing and destroying 10,000,000 Integer objects in a tight loop is extremely CPU intensive compared to using raw int primitives.

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

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.

  1. Create a string array: {"80", "76", "bad", "91"}.
  2. Loop through each value.
  3. Use Integer.parseInt inside try-catch.
  4. Skip invalid values.
  5. Calculate the average of valid marks only.

Goal: Practice wrapper parsing, invalid input handling, and primitive arithmetic after parsing.

Revision Checkpoint

Before the quiz: Explain why ArrayList<int> is invalid but ArrayList<Integer> works.

Open on QuizMaker