Try this 🤔
Integer a = 100; Integer b = 100;
System.out.println( a == b) // true
Integer x = 200; Integer y = 200; // false
you know why?! #Java#Programming 👇
Welcome to the Integer Pool!
Java has a secret: the Integer Pool! Think of it like a special VIP lounge for small, frequently used numbers (typically -128 to 127). Java pre-creates and reuses these exact Integer objects to be super efficient.
Why Bother?
There are two big reasons for developers:
Memory Saving: No need to create new objects for the number 5 a million times.
Performance Boost: Less object creation overhead.
It's Java being smart with resources! Does your code need this kind of optimization?
How it Works (Autoboxing)
When you use Integer.valueOf(10) or simply Integer i = 10; (autoboxing), Java peeks into this lounge. If 10 is there, it hands you the existing object. If it's 200, it says, "Sorry, you need a new one!"
The Tricky Part: == vs .equals()
This is where == gets tricky! == checks if two references point to the exact same object. For numbers in the pool, they often do. Outside the pool, even if values are identical, they're separate objects. Always use .equals() for value comparison