Wrapper Classes: Boxing Primitives as Objects
What you'll learn: How Java wraps primitive values inside objects so they can be used where reference types are required.
Why Wrapper Classes Exist
You've learned that primitives (int, double, boolean, etc.) and reference types are fundamentally different. Primitives live on the stack and aren't objects—they have no methods and can't be null. But sometimes you need primitive values to behave like objects.
Wrapper classes solve this problem. They are special reference types that "wrap" a primitive value inside an object, giving it all the properties of a reference type.
The Core Wrapper Classes
Java provides a wrapper class for each primitive type:
IntegerwrapsintDoublewrapsdoubleBooleanwrapsbooleanCharacterwrapscharLongwrapslongFloatwrapsfloatBytewrapsbyteShortwrapsshort
Think of It Like a Gift Box
Imagine a primitive as a coin sitting on a table. A wrapper class is like putting that coin inside a jewelry box. The coin's value hasn't changed, but now it's packaged as an object you can label, store differently, or pass around in contexts that require "boxed" items.
When You'll Use Them
Wrapper classes become essential when:
- Collections or data structures require objects (not primitives)
- You need
nullto represent "no value" - You want utility methods for converting or parsing values
- APIs demand reference types instead of primitives
Key Takeaway: Wrapper classes like Integer, Double, and Boolean turn primitives into objects, allowing them to be used wherever reference types are needed while preserving their underlying values.