Samadrita Shaw
The more I share, the more I learn.
Very nicely written!
There's another trick that's guaranteed to work and would arguably be more performant:
str.intern() == "Interned String"
The reason == sometimes works is the interned string pool inside String. The intern() method makes sure the string is properly stored there and returns the interned instance. This way you can use pointer arithmetic which is faster. Notice that hardcoded strings are always interned so you can rely on that.
The one big downside of this is that most developers and tools looking at == code will assume it's a bug.
When working with string builder .equals() method apparently doesn't work.
In order to make it work you first need to convert the string builder to string by using .toString() and then use .equals().
Eg:
sb1.toString().equals(sb2.toString());
Everything else is just perfect and I felt like adding this piece of information. π
Sayantani Dey
It is the errors that lead to insightful study
The explanation was so clear, looking forward to your blogsβ€οΈ