
★ Sr Backend Developer | 9+ years Experience ★ Sharing my experience via tweets & threads ★ Explains complex concepts
Punjab
Joined May 2022
- Tweets2.1K
- Following126
- Followers1.4K
- Likes611
In Action
Integer num1 = 50;
Integer num2 = 50;
System.out.println(num1 == num2); // true (pooled!)
Integer num3 = 150;
Integer num4 = 150;
System.out.println(num3 == num4); // false (not pooled!)
29
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
1
37
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!"
1
4
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?
1
5
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.
1
8
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 👇
1
49
Key Takeaways:
Java's String Pool reuses literals.
new String() always creates new objects.
intern() forces pooling.
== vs .equals() is crucial for comparison
32
Classic Gotcha: `==` vs `.equals()`
Comparing user input (often dynamically created Strings) with constant literals using == will likely fail! ❌ Always use .equals() for content comparison. Remember: == for reference, .equals() for value.
1
36
Why care about this obscure String Pool? Huge Performance & Memory wins! 🚀 Reusing String literals drastically reduces heap consumption and garbage collection overhead in large applications. Imagine the savings! Are your services missing out?
1
7
Forcing Strings into the Pool
Want to force a dynamically created String into the pool? Use String.intern(). This method checks the pool; if the String isn't there, it adds it and returns its reference. If it is, it returns the pooled reference.
But why use it? ✨
1
7
The `new String()` Difference
But String s = new String("literal"); is different! This always forces a NEW String object creation in the heap, even if 'literal' is already in the pool. That's why new String("A") == new String("A") is always false. Two distinct objects.
1
7
When you declare String s = "literal";, Java's String Pool (a special memory area in the heap) kicks in. It checks if 'literal' already exists. If yes, it reuses the existing object. If no, it creates one and puts it there. Smart memory saving, right? 🤔
1
12
Ever wonder why "hello" == "hello" is true, but new String("hello") == new String("hello") is false? Let's unravel this 👇
The String Pool
1
47
Q: How do you share variable in thread life cycle?
A: Java ThreadLocal class allows the storage of data that is specific to each thread separately.
It is often used to implement thread-specific context or to avoid synchronisation for thread-local data
4
1,093
Replying to @Shubh28659122
True, Threads are a bit complicated, but if you understand the concept, they're a lot of fun.
42

