Wrapper classes allow primitive types like int, double, etc., to be treated as objects. This is useful when you need to use primitives in collections or methods that require objects.
Integer a = 1000; Integer b = 1000; System.out.println(a == b);
Java caches Integer objects for values between -128 and 127. Since 1000 is outside this range, a and b refer to different objects, so '==' returns false.
import java.util.List; import java.util.ArrayList; List<Integer> list = new ArrayList<>(); list.add(10); list.add(null); System.out.println(list.get(1) == null);
The list stores Integer objects, and adding null is allowed. Retrieving the second element returns null, so comparing with null using '==' returns true.
Java generics are designed to work with reference types (objects). Primitive types like int cannot be used directly, so their wrapper classes are used instead.
int x = null; Integer y = null; System.out.println(x + y);
Primitive types like int cannot be assigned null. The line 'int x = null;' causes a compilation error due to incompatible types.
