Wrapper Classes in Java – Integer, Double, Boolean, and More

Illustration for Wrapper Classes in Java – Integer, Double, Boolean, and More
By Last updated:

Introduction

Java provides wrapper classes to treat primitive types as objects. Each primitive type (int, double, boolean, etc.) has a corresponding wrapper class (Integer, Double, Boolean).

Why It Matters

  • Enables primitives to work with collections and generics.
  • Provides utility methods for type conversion and parsing.
  • Bridges the gap between primitive and object-oriented programming.

When to Use

  • When working with frameworks or APIs requiring objects (e.g., Collections).
  • When you need to convert primitives to strings and vice versa.
  • When using null to represent absence of value (not possible with primitives).

Core Concepts

Primitive vs Wrapper Classes

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

Autoboxing & Unboxing

  • Autoboxing: Automatic conversion of primitives to wrappers.
  • Unboxing: Automatic conversion of wrappers to primitives.
Integer num = 10;     // Autoboxing (int -> Integer)
int value = num;      // Unboxing (Integer -> int)

Real-World Analogy

Think of primitives as raw ingredients (flour, sugar) and wrapper classes as packaged goods. Packaging allows you to transport, store, and use them in more flexible contexts (collections, APIs).


Real-World Use Cases

  • Collections: List<Integer> instead of List<int> (generics only support objects).
  • Parsing: Integer.parseInt("123") converts strings to numbers.
  • Null Handling: Databases often use null, requiring wrapper classes.

Common Mistakes & Anti-Patterns

  1. Excessive Autoboxing:

    • Can degrade performance in loops.
    Integer sum = 0;
    for (int i = 0; i < 1000; i++) sum += i; // Repeated boxing/unboxing
    
  2. Comparing Wrappers with ==:

    • Use .equals() to avoid reference comparison pitfalls.
    Integer a = 100, b = 100;
    System.out.println(a == b); // true (cached)
    Integer x = 200, y = 200;
    System.out.println(x == y); // false
    
  3. Using Wrappers when primitives suffice:

    • Adds unnecessary overhead.

Performance & Memory Implications

  • Wrappers consume more memory than primitives.
  • Autoboxing creates temporary objects, impacting performance.
  • Caching exists for some wrapper values (e.g., Integer -128 to 127).

Tuning Tips

  • Use primitives in performance-critical loops.
  • Cache or reuse wrapper objects where possible.

Best Practices

  • Use primitives unless wrappers are required.
  • Be cautious with autoboxing in large loops.
  • Always use .equals() for comparison.
  • Leverage utility methods (parseInt, valueOf) for conversions.

Java Version Relevance

Version Change
Java 5 Introduced autoboxing/unboxing
Java 7+ Improved Integer caching
Java 8+ Enhanced Optional with wrapper types

Code Examples

Parsing & Conversion

String numStr = "123";
int num = Integer.parseInt(numStr);       // String -> int
String s = Integer.toString(num);         // int -> String
Double d = Double.valueOf("12.34");       // String -> Double

Collections with Wrappers

List<Integer> numbers = new ArrayList<>();
numbers.add(10);  // Autoboxing
numbers.add(20);
int sum = 0;
for (Integer n : numbers) sum += n; // Unboxing

Conclusion & Key Takeaways

  • Wrapper classes bridge primitives with object-oriented features.
  • Autoboxing simplifies code but can impact performance.
  • Always use .equals() for comparing wrapper objects.
  • Use primitives for high-performance scenarios; wrappers for collections and nullability.

FAQ

  1. Why do we need wrapper classes in Java?
    To use primitives in object-oriented contexts like collections.

  2. What is autoboxing?
    Automatic conversion of primitives to wrapper objects.

  3. Is unboxing automatic too?
    Yes, the reverse of autoboxing.

  4. Which values are cached in Integer?
    Default range is -128 to 127.

  5. Are wrapper classes immutable?
    Yes, all wrapper classes are immutable.

  6. Can I use == to compare wrapper objects?
    Avoid it; use .equals().

  7. Do wrapper classes affect performance?
    Yes, they add memory and CPU overhead.

  8. When should I prefer primitives over wrappers?
    In performance-sensitive and non-nullable cases.

  9. Can wrapper classes be null?
    Yes, unlike primitives.

  10. Which Java version introduced autoboxing?
    Java 5.