Java Wrapper Classes Explained: Autoboxing, Unboxing, Integer Cache Pitfalls & Common APIs
The article explains Java's primitive types versus object types, introduces wrapper classes such as Integer, Double and Boolean, demonstrates manual and automatic boxing/unboxing, reveals the -128~127 Integer cache trap, shows common wrapper APIs, and warns about null‑unboxing NPEs.
Introduction
Java has eight primitive types (int, double, boolean, …) but collections store only objects, so each primitive has a corresponding wrapper class.
Boxing and Unboxing
Manual boxing/unboxing
// manual boxing
Integer a = Integer.valueOf(100);
// manual unboxing
int b = a.intValue();Automatic boxing/unboxing (JDK 5+)
// automatic boxing – compiler calls Integer.valueOf()
Integer x = 100;
// automatic unboxing – compiler calls x.intValue()
int y = x;Integer caches values in the range –128 to 127. Within this range == returns true; outside the range a new object is created and == returns false. Use equals for logical comparison.
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1 == i2); // true (cached)
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i3 == i4); // false (outside cache)
System.out.println(i3.equals(i4)); // trueCommon Integer Methods
Integer.parseInt(String)– converts a string to int. Integer.valueOf(int) – converts an int to an Integer object. Integer.toString(int) – converts an int to a String. Integer.toBinaryString(int) – returns a binary representation. Integer.toHexString(int) – returns a hexadecimal representation. Integer.MAX_VALUE – 2147483647. Integer.MIN_VALUE – –2147483648. Integer.compare(int x, int y) – compares two int values.
Code Demonstration
public class WrapperDemo {
public static void main(String[] args) {
// 1. String to number (common when reading user input)
String numStr = "42";
int num = Integer.parseInt(numStr);
System.out.println(num + 1); // 43
// 2. Number to string
String str = Integer.toString(100);
System.out.println(str.length()); // 3
// 3. Base conversion
System.out.println(Integer.toBinaryString(10)); // 1010
System.out.println(Integer.toHexString(255)); // ff
// 4. Max/min constants
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.MIN_VALUE); // -2147483648
// 5. Storing wrappers in a collection
java.util.List<Integer> list = new java.util.ArrayList<>();
list.add(1); // auto‑boxing
list.add(2);
int sum = 0;
for (int val : list) { // auto‑unboxing
sum += val;
}
System.out.println("sum = " + sum); // sum = 3
}
}Null‑Pointer Pitfall
Integer value = null;
// Dangerous: automatic unboxing of null throws NullPointerException
int result = value; // NPE!
// Safe: check for null before unboxing
if (value != null) {
int result2 = value;
}Wrapper objects can be null; always check for null before unboxing to avoid NullPointerException.
Common Pitfalls Summary
Never use == to compare wrapper objects; use equals instead.
Unboxing a null wrapper throws NullPointerException. Integer.parseInt() throws NumberFormatException for non‑numeric input.
The cache range –128 to 127 may make == appear true; do not rely on this behavior.
Learning Summary
Boxing/unboxing → automatic after JDK 5, backed by valueOf() and xxxValue()
Integer → parseInt for numbers, toString for strings, convenient base conversions
Key points → use equals for comparison, null‑check before unboxing, beware of cache trapSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
