Which statement about int and Integer is correct?
Options
- They are the same type, written two different ways
intis a primitive and cannot be null;Integeris an object and can (Correct option)Integeris always faster thanintbecause it is an objectintcan be a generic type argument, as inList<int>
Answer
int is a primitive (cannot be null); Integer is an object wrapper (can be null)
Why
Java has 8 primitive types (int, long, double, boolean, char, byte, short, float). They hold a plain value and can never be null.
Each primitive has a wrapper class (Integer, Long, Double...) that is a real object. It can be null and can be used in collections and generics: List<Integer> works, List<int> does not.
Java converts between them automatically (autoboxing/unboxing). Unboxing a null Integer throws NullPointerException.
Remember
Primitive = raw value. Wrapper = object (can be null).