The process of combining identical strings is called “interning”, and has been done for many years by lots of language compilers, but not always. The answer to the question, especially as expanded by @GennadyVanin–Novosibirsk, depends on the language and the compiler implementation. For Java, all constant strings are interned, as required by the Java Language Specification. But that’s only constant string expressions, and only when they’re compiled at the same time. If you have two Java strings sufficiently separated in time and space (e.g., compiled into separate JAR files), they will not be the same object. Similarly, dynamically created Java strings (e.g., the output of various toString()
methods) won’t be interned unless the method specifically requests it via String.intern()
. And yes, all uses of an interned string will share the same memory locations – that’s a big part of why strings are interned in the first place.
As to other languages, that’s a bigger question, but with all the information in these answers, I’m sure you can research it on the web. Suffice it to say that there is no universal agreement on how this ought to be done.