By reading Oracle JVM architecture document:
A run-time constant pool is a per-class or per-interface run-time
representation of the constant_pool table in a class file (§4.4).
String value = "abc"
This is a preemptive optimization that the JLS superimposes.
From JLS 7, §3.10.5 (formatting mine)
Moreover, a string literal always refers to the same instance of class
String. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method
However, note that this is only true of String literals and constant expressions. Dynamically constructed strings (e.g.
x + y for Strings
y) are not automatically interned to share the same unique instances. As a result, you will still have to use
.equals in general unless you can guarantee that your operands are constant expressions.