What are wrapper classes and autoboxing? Why is a == b false for Integer a = 128, b = 128?
Short answer
Autoboxing is Java turning an int into an Integer for you. Numbers from -128 to 127 are reused (cached), so 127 == 127 is true but 128 == 128 is false.
3 points to say:
- Compare
Integervalues withequals. - Turning
nullinto anintthrowsNullPointerException. - For large numeric data use
int, notInteger.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.