I remember reading a section, possibly in Bloch's Effective Java, that said that for most cases, where
String a = "fish";
String b = "fish";
that a == b in most cases because Strings are immutable. But that due to temporary construction of objects or some such, new String("fish") would yield a distinct object reference.
I looked through Bloch chapters on equals(), immutability, and object creation, but cannot find this bit I remember!! Tearing my hair out, does anyone remember where is the description of why this is? It may not even be in EJ but I'd like to find it. Hint: where is this explained is my actual question.
It's not related to immutability. It's the way strings are handled by the JVM. A string literal with the same contents represents the same object ("string literal" means roughly "text surrounded by quotes"). There is a table of string objects in the JVM, and each string literal has exactly one object in that table.
However, when you expicitly create a new instance, you construct a new string object based on the string object taken from the table.
From any string formed by not using a literal (but by calling toString(), by instantiating, etc.) you can get the object from the jvm table by calling str.intern()
. The intern()
method returns exactly one instance for each character sequence that exists. new String("fish").intern()
will return the same instance as simply String s = "fish"
There are two things to remember:
new String("something")
equals(..)
(unless you really know what you are doing, and document it)