TOPIC 4.7 · STUDY
Wrapper Classes
Integer and Double are objects that wrap an int or a double. Autoboxing builds a wrapper from a primitive; unboxing pulls the primitive out for arithmetic or assignment. Integer.parseInt and Double.parseDouble turn digit text into numbers. Wrapper objects are immutable, and unboxing null throws NullPointerException.
4.7.A
Integer and Double wrapper classes
A primitive such as int or double is a bare value. Integer and Double are classes whose objects hold one of those values. An Integer stores an int; a Double stores a double. Both live in java.lang, so neither needs an import.
Autoboxing is the automatic conversion from a primitive to a matching wrapper. Writing Integer stock = 14; stores an Integer object that holds 14. The same idea works for Double: Double rate = 1.25; boxes the double. Autoboxing also happens when an int is passed to a method that expects Integer, when a method returns Integer from an int expression, or when an int is added to an ArrayList of Integer.
Autoboxing wraps the int 14 inside an Integer. Unboxing reads that stored int back out.
Unboxing is the reverse: Java takes the int or double out of the wrapper. Assigning Integer to int, or Double to double, unboxes. Using a wrapper in arithmetic also unboxes, then the result can autobox again if it is stored in a wrapper variable.
Integer stock = 14; // autobox int to Integer
int ready = stock; // unbox Integer to int
System.out.print(ready + 3); // 17
stock = stock + 5; // unbox, add, autobox new Integer
System.out.print(stock); // 19
Double rate = 1.25;
double half = rate / 2; // unbox for the division
System.out.print(half); // 0.625
Integer.parseInt turns a String of digits into an int. Double.parseDouble turns a String into a double. Those methods return primitives. If you assign the result to Integer or Double, autoboxing builds the wrapper afterward. Parsing is not the same as autoboxing: parseInt("26") yields the int 26, not an Integer by itself.
int doors = Integer.parseInt("18");
double span = Double.parseDouble("2.5");
System.out.print(doors + 2); // 20
System.out.print(span + 1); // 3.5
Integer boxed = Integer.parseInt("18"); // int result, then autobox
Wrapper objects are immutable. An expression such as stock = stock + 5 does not change the old Integer in place. It unboxes, computes a new value, and stores a new Integer in the variable. Reassigning a method parameter that is Integer or Double only rebinds that local copy of the reference; the caller's variable still points at the original object.
public void nudge(Integer n) {
n = n + 1; // local n now refers to a new Integer
}
Integer count = 4;
nudge(count);
System.out.print(count); // still 4
A wrapper variable may hold null, meaning it refers to no object. Unboxing null fails at run time with a NullPointerException. The assignment can compile; the crash happens when Java tries to read a primitive from nothing.
Key points
- Integer wraps an int; Double wraps a double. Both are in java.lang.
- Autoboxing converts a primitive into a wrapper object automatically.
- Unboxing converts a wrapper into a primitive for assignment or arithmetic.
- Integer.parseInt and Double.parseDouble return primitives from text.
- Wrappers are immutable; updates store a new object, not a mutated one.
- Unboxing a null wrapper throws NullPointerException.
A null Integer cannot be unboxed. Assigning Integer gap = null; then writing int n = gap; throws NullPointerException at run time.