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.