Member-only story
Interviewer asked me why is 1 == 1 True but 1000 == 1000 False in Java?
The maddening truth about Java’s Integer caching and why it loves small numbers but betrays you at bigger ones.
I remember the first time I saw this in action.
I had just wrapped my head around autoboxing in Java and thought, “Great, I can treat primitives and objects almost the same. Life is good.”
And then, out of nowhere, my neat little test failed.
Integer a = 1000;
Integer b = 1000;
System.out.println(a == b); // false
Integer x = 1;
Integer y = 1;
System.out.println(x == y); // trueMy reaction? Pure rage.
How the heck is 1 == 1 true but 1000 == 1000 false?
Isn’t equality supposed to be straightforward?
Spoiler: Java doesn’t care about your feelings. It cares about performance.
The Great Betrayal: Autoboxing and Identity
Let’s get one thing straight: == on objects doesn’t check value equality—it checks reference equality.

