Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Layman Explanation
Primitives are raw ingredients (flour, sugar). Wrapper classes are the same ingredients in a packaged box — same content but now comes with labels, instructions, and extra features (methods).
Primitive to Wrapper Mapping
javascript
QUBITS OF DPK
Autoboxing & Unboxing
java
QUBITS OF DPK
Why Wrapper Classes Exist
java
QUBITS OF DPK
Integer Cache — Famous Interview Trap
java
QUBITS OF DPK
️ Always use .equals() for wrapper comparison — never ==
NullPointerException with Unboxing
java
QUBITS OF DPK
Performance Note
java
QUBITS OF DPK
Interview Answer (SDE-2)
"Wrapper classes wrap primitives as Objects to use them in contexts requiring objects — collections, generics, null assignment. Autoboxing automatically converts primitive to wrapper; unboxing converts back. Integer caches -128 to 127 so == works in that range but fails outside — always use equals() for wrapper comparison. Unboxing null throws NullPointerException — always null check first. Wrapper methods like Integer.parseInt(), Character.isDigit() are utility methods heavily used in DSA."
Interview Questions & MAANG-Level Answers
Q1. What is a wrapper class and why does it exist?
Wrapper classes (Integer, Double, Boolean etc.) wrap primitive values as objects. Why: (1) Collections require objects — ArrayList<Integer> not ArrayList<int>. (2) Generics require objects — Optional<Integer>, CompletableFuture<Integer>. (3) Null support — Integer can be null (useful for optional DB fields), int cannot. (4) Utility methods — Integer.parseInt(), Integer.toBinaryString(), Integer.MAX_VALUE, Character.isDigit().
Q2. What is autoboxing and unboxing?
Autoboxing: automatic conversion from primitive to wrapper: Integer i = 5 — Java inserts Integer.valueOf(5). Unboxing: automatic conversion from wrapper to primitive: int x = i — Java inserts i.intValue(). Happens in: assignments, method arguments, arithmetic operations with mixed types. Performance note: avoid in tight loops — each autobox creates a new object (except cached range). Long sum = 0L; for(long i=0; i<1M; i++) sum += i; — constant boxing degrades performance significantly.
Q3. Why does Integer 127 == 127 return true but 128 == 128 return false?
Java's IntegerCache caches Integer objects from -128 to 127. Integer.valueOf(127) always returns the same cached object, so == (reference comparison) is true. Integer.valueOf(128) creates a new object each time, so == compares different references — false. This is specified in the Java Language Specification. Always use .equals() for Integer comparison: a.equals(b) works correctly for ALL values.
Q4. What is the danger of unboxing a null wrapper?
NullPointerException! Unboxing a null wrapper automatically calls .intValue() on null:
java
QUBITS OF DPK
Always null-check before unboxing: int x = (count != null) ? count : 0; or use Objects.requireNonNullElse(count, 0).
Q5. When would you use Integer instead of int for a field?
Use Integer (object) when: (1) The field can be absent/unknown — Integer age = null represents "age not provided". (2) Working with Collections or Generics. (3) JPA/Hibernate entity fields — Integer id allows null before persistence. (4) JSON deserialization where field may be missing. (5) Method return type where null means "not found". Use int (primitive) when: value always exists, performance-critical code, avoiding boxing overhead in calculations.