Wrapper

An object representation of a primitive type (like int, double, char, etc.)

  • Used when u need to use primitive types as if they were reference types
  • 💡 The term “wrapper” signifies that it “wraps” a primitive value into an object form.
Primitive ClassWrapper class (래퍼 클래스)
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Wrapper class instances can be made with constructors or literals

Integer i = new Integer(123); // constructor
Integer i = 123; // literals
 
System.out.println(123 == 123); // true
System.out.println(new Integer(123) == new Integer(123)); // false
// they both point to different instances -> have to use .equals
System.out.println(new Integer(123).equals(new Integer(123)));

Boxing and Unboxing

Boxing and Unboxing

  • Boxing: Converting a primitive type into its corresponding Wrapper Class object.
  • Unboxing: Converting a Wrapper Class object back into its primitive type.
  • AutoBoxing / AutoUnboxing
    • Since JDK 1.5, the compiler automatically handles these conversions for you

Comparing Values & Caching

Integer num1 = 100; // Autoboxed
Integer num2 = 100; // Autoboxed
Integer num3 = 200; // Autoboxed
Integer num4 = 200; // Autoboxed
 
System.out.println(num1 == num2); // True for small values (due to Integer caching)
System.out.println(num3 == num4); // False for larger values (new objects are created)
 
System.out.println(num1.equals(num2)); // True (compares values)
System.out.println(num3.equals(num4)); // True (compares values)
  • For Integer objects with values between -128 and 127 (inclusive), Java often caches these objects
    • if you declare Integer num1 = 100; and Integer num2 = 100;, num1 and num2 might actually refer to the same object in memory, leading == to return true
  • However, for values outside this range, new objects are typically created, and == would return false even if the values are the same
  • Always use equals() to compare the values of Wrapper Class objects to avoid unexpected behavior!
ClassCaching RangeIs Caching Performed?
ByteAll Values
Short-128 ~ 127
Integer-128 ~ 127
Long-128 ~ 127
Character0 ~ 127 (ASCII)
Booleantrue / false
FloatNone
DoubleNone

With Collections

class StringListWrapper {
    private List<String> list = new ArrayList<>();
 
    public void add(String value) {
        list.add(value);
    }
 
    public String get(int index) {
        return list.get(index);
    }
}