Language Features
Wrapper Classes
Complete lesson for Wrapper Classes using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Wrapper Classes
Section: Language Features. Byte, Integer, Double, Boolean, autoboxing, the Integer cache, and parsing APIs.
Why wrappers exist
- Primitives are not objects. Collections and generics need reference types:
List, notList. - Each primitive has a wrapper:
Byte,Short,Integer,Long,Float,Double,Character,Boolean.Voidis the placeholder forvoid.
| Primitive | Wrapper | Parse / box helpers |
|---|---|---|
int | Integer | parseInt, valueOf, intValue |
double | Double | parseDouble, valueOf |
boolean | Boolean | parseBoolean, valueOf |
char | Character | isDigit, toUpperCase (static) |
Autoboxing and unboxing
- Java 5+ converts
int↔Integerautomatically:Integer x = 5;andint y = x;. Integer.valueOf(int)is preferred overnew Integer(int)(deprecated).parseIntreturns a primitive;valueOf(String)returns a wrapper.
public class WrapperBasics {
public static void main(String[] args) {
Integer boxed = 42; // autobox
int raw = boxed; // unbox
int parsed = Integer.parseInt("42");
Integer cached = Integer.valueOf(42);
System.out.println(raw + parsed + cached);
}
} The Integer cache
Integer.valueOf(i)(and autoboxing) reuses cached objects for **-128 to 127** inclusive. Outside that range, a new object is typical.- So
Integer a = 100; Integer b = 100; a == bis true, butInteger c = 200; Integer d = 200; c == dis often false. Always useequals.
Advanced — Wrapper Classes
Null unboxing, compare, and identity
- Unboxing
nullthrowsNullPointerException:Integer x = null; int y = x;. - Do not mix
==on wrappers. UseequalsorintValue()/compare.Integer.compare(a, b)is null-hostile if you pass wrappers that are null. Boolean.parseBoolean("true")is case-insensitive; any other string isfalse(no exception).- Optional cache size for
Integercan be widened with-XX:AutoBoxCacheMax, but never depend on it in code.
Tricky Interview Points
Q1) Why do we need wrapper classes?
Answer To treat primitives as objects: collections, generics, reflection, and APIs that require Object.
Q2) parseInt vs valueOf?
Answer Integer.parseInt(String) returns int. Integer.valueOf(String) returns Integer (and may use the cache).
Q3) Why can Integer 127 == Integer 127 be true but 128 be false?
Answer valueOf caches -128..127. == tests reference identity. 128 usually allocates two objects.
Q4) What happens when you unbox null?
Answer NullPointerException.
Q5) Is autoboxing free?
Answer No. It allocates (except cached values) and can hide NPEs and extra GC. Prefer primitives in tight loops.
Q6) Can you store int in ArrayList?
Answer You write ArrayList<Integer>. Autoboxing inserts Integer objects. You cannot write ArrayList<int> in standard Java.
Simple Programs
Cache vs equals
public class IntegerCacheDemo {
public static void main(String[] args) {
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println(a == b); // true (cache)
System.out.println(c == d); // false (typically)
System.out.println(c.equals(d)); // true
Integer missing = null;
System.out.println(missing == null);
}
} Problem Solving Tricks
- Never use
==on wrappers.equalsor compare primitives after a null check. - In streams,
mapToIntavoids boxing when you needint. Characterhas noparseChar; uses.charAt(0)after validating length.Boolean.getBoolean("x")reads a *system property*, not the string"x"— a famous trap. UseparseBoolean.
Practice Programs List
- Box and unbox each primitive type once and print them.
- Loop
Integer.valueOf(i)from 120 to 130 and print==against a secondvalueOf(i). - Parse
"3.14"todoubleand handleNumberFormatExceptionfor"pi". - Write a method
int safeUnbox(Integer x, int fallback). - Sum an
ArrayListthat contains anulland fix the NPE.