Sitemap
JavaGuides

Guides on Java, Spring Boot, REST APIs, Full-Stack Web development, Microservices, Cloud, Databases, and tools with hands-on tutorials and best practices.

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.

3 min readAug 17, 2025

--

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); // true

My 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.

Non members can read full article by clicking here!

Press enter or click to view image in full size
Image Edited on Canva Pro

The Great Betrayal: Autoboxing and Identity

Let’s get one thing straight: == on objects doesn’t check value equality—it checks reference equality.

--

--

JavaGuides
JavaGuides

Published in JavaGuides

Guides on Java, Spring Boot, REST APIs, Full-Stack Web development, Microservices, Cloud, Databases, and tools with hands-on tutorials and best practices.

Harry
Harry

Written by Harry

Java Developer | Writing Code, Solving Problems, Building Reliable Software | Love to Write

Responses (68)