The more I share, the more I learn.
I am junior, pursuing bachelors in Computer Science and Engineering. Currently I am strengthening my DSA in Java and have started learning HTML and CSS to move forward in the path of web development.
Markdown supported · ⌘↵ to post
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. 😊
Thank you so much for sharing this! This is something new for me. 🙌
Sayantani Dey
The explanation was so clear, looking forward to your blogs❤️