Skip to main content

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.

L1

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, not List.
  • Each primitive has a wrapper: Byte, Short, Integer, Long, Float, Double, Character, Boolean. Void is the placeholder for void.
PrimitiveWrapperParse / box helpers
intIntegerparseInt, valueOf, intValue
doubleDoubleparseDouble, valueOf
booleanBooleanparseBoolean, valueOf
charCharacterisDigit, toUpperCase (static)

Autoboxing and unboxing

  • Java 5+ converts int ↔ Integer automatically: Integer x = 5; and int y = x;.
  • Integer.valueOf(int) is preferred over new Integer(int) (deprecated). parseInt returns 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 == b is true, but Integer c = 200; Integer d = 200; c == d is often false. Always use equals.
L2

Advanced — Wrapper Classes

Null unboxing, compare, and identity

  • Unboxing null throws NullPointerException: Integer x = null; int y = x;.
  • Do not mix == on wrappers. Use equals or intValue() / compare. Integer.compare(a, b) is null-hostile if you pass wrappers that are null.
  • Boolean.parseBoolean("true") is case-insensitive; any other string is false (no exception).
  • Optional cache size for Integer can be widened with -XX:AutoBoxCacheMax, but never depend on it in code.
L3

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.

L4

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);
    }
}
L5

Problem Solving Tricks

  • Never use == on wrappers. equals or compare primitives after a null check.
  • In streams, mapToInt avoids boxing when you need int.
  • Character has no parseChar; use s.charAt(0) after validating length.
  • Boolean.getBoolean("x") reads a *system property*, not the string "x" — a famous trap. Use parseBoolean.
L6

Practice Programs List

  1. Box and unbox each primitive type once and print them.
  2. Loop Integer.valueOf(i) from 120 to 130 and print == against a second valueOf(i).
  3. Parse "3.14" to double and handle NumberFormatException for "pi".
  4. Write a method int safeUnbox(Integer x, int fallback).
  5. Sum an ArrayList that contains a null and fix the NPE.