Primitives, Wrappers and Autoboxing Questions
45 min · Beginner
Java keeps eight primitive types as plain values for speed, and wraps each in a wrapper class when an object is needed. The conversion between them happens automatically, which makes code shorter and creates the Integer-cache and null-unboxing traps that interviewers love.
Topics in this lesson
- Recap: eight primitives and their wrappers
- Autoboxing and unboxing
- Predict the output: the Integer cache
- NullPointerException from unboxing null
- Common questions: why wrappers, int vs Integer, char arithmetic
Interview questions this lesson answers
- What are the eight primitive types in Java?
- What are the sizes of the primitive types?
- What are the default values of primitives?
- What is a wrapper class?
- Why do we need wrapper classes?
- What is autoboxing?
- What is unboxing?
- Why does
Integer a = 127, b = 127; a == bprint true but the same with 128 prints false? - How should you compare two Integer objects?
- Which wrapper classes have a cache?
Sign in free to continue this lesson
Create a free account to read every lesson, practise with the exercises and keep track of your progress.