← Back to Home

Wrapper Classes

Wrapper classes convert primitive data types into objects. They are essential when working with collections, generics, frameworks, and APIs that operate on objects rather than primitives.

What Are Wrapper Classes?

  • Object representations of primitive data types
  • Located in the java.lang package
  • Provide utility methods for conversion and parsing
  • Enable object-based operations on primitive values

Why it matters: Java collections and generics cannot store primitives—they require objects.

Primitive vs Wrapper Mapping

Primitive Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Example

int a = 10;          // primitive
Integer b = 10;      // wrapper object
          

Why Wrapper Classes Are Needed

  • Collections store objects (List, Set, Map)
  • Generics work only with objects
  • Frameworks (Hibernate, Spring) expect objects
  • Provide parsing and conversion utilities

Example (Collections)

List<Integer> list = new ArrayList<>();
list.add(10);  // primitive auto-converted
          

Creating Wrapper Objects

  1. Using Constructor (Deprecated)
    Integer i = new Integer(10);  // deprecated
                  
  2. Using valueOf() (Recommended)
    Integer i = Integer.valueOf(10);
                  
  3. Using Autoboxing (Modern Java)
    Integer i = 10;
                  

Autoboxing & Unboxing (Conceptual)

  • Autoboxing → Primitive → Wrapper
  • Unboxing → Wrapper → Primitive
Integer a = 10;  // autoboxing
int b = a;       // unboxing
          

Why it matters: Simplifies code but can impact performance in loops.

Common Utility Methods

Parsing Strings to Primitives

int x = Integer.parseInt("100");
double d = Double.parseDouble("12.5");
boolean flag = Boolean.parseBoolean("true");
          

Value Extraction

Integer i = 20;
int x = i.intValue();
          

Constants in Wrapper Classes

Integer.MAX_VALUE
Integer.MIN_VALUE
          

Use case: Validation and boundary checks.

Wrapper Classes and Null Handling

  • Wrapper objects can be null
  • Unboxing a null wrapper causes NullPointerException
Integer x = null;
int y = x;  // ❌ NullPointerException
          

Why it matters: Common real-time bug source.

Performance Considerations

  • Primitives are faster and memory-efficient
  • Wrappers add object overhead
  • Avoid excessive autoboxing in loops
for (int i = 0; i < 1000; i++) {
    list.add(i); // autoboxing happens
}
          

Primitive vs Wrapper (Comparison)

Feature Primitive Wrapper
Type Value Object
Null Allowed ❌ No ✅ Yes
Collections ❌ No ✅ Yes
Performance Faster Slower
Methods ❌ No ✅ Yes

Common Beginner Mistakes

  • Assuming wrapper objects behave like primitives
  • Ignoring null checks before unboxing
  • Overusing wrappers unnecessarily
  • Using deprecated constructors
  • Forgetting performance impact

Interview-Ready Answers

Short Answer

Wrapper classes are object representations of primitive data types used to work with collections, generics, and object-based APIs.

Detailed Answer

Java wrapper classes such as Integer and Double wrap primitive values into objects. They enable primitives to be used in collections and provide utility methods for parsing, conversion, and value manipulation.

Key Takeaway

Wrapper classes bridge primitives and objects in Java. Use them where object behavior is required, but prefer primitives for performance-critical logic.