Java Integer caching: true vs false

💡 Question: What will be the output of this Java code? public class Test { public static void main(String[] args) { Integer a = 100; Integer b = 100; Integer c = 200; Integer d = 200; System.out.println(a == b); System.out.println(c == d); } } 🤔 Think before answering. Don’t rush. Most people say the same output for both lines… and that’s where the trap is. 👇 Actual Output true false 🔥 Why this happens? (The twist) Java caches Integer objects in the range -128 to 127 a and b point to the same cached object c and d are different objects in heap memory == compares references, not values 👉 If this surprised you, imagine this bug hiding in a production system 😅 📌 Lesson for Interviews & Real Code Always use .equals() for wrapper classes Understand autoboxing, caching, and JVM internals Core Java ≠ basic Java 💬 Comment your answer before reading the explanation ❤️ Like if Java still manages to trick you after years 🔁 Repost to challenge your Java network #Java #CoreJava #JavaInterview #JVM #Programming #SoftwareEngineering #JavaDeveloper #LinkedInLearning

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories