1From the Java Language Specification: “A string literal consists of zero or more characters enclosed in double quotes. Each character may be represented by an escape sequence.”
The “constant pool” is something that is computed at compile time and stored with the compiled .class file. It contains information on methods, classes, interfaces, …, and string literals. When the JVM loads the .class file and the s0 and s2 variables are resolved, the JVM does something called constant pool resolution. The process of constant pool resolution for string follows these steps, as taken from the Java Virtual Machine Specification (5.4):
- • If another constant pool entry tagged CONSTANT_String2 and representing the identical sequence of Unicode characters has already been resolved, then the result of resolution is a reference to the instance of class String created for that earlier constant pool entry.
2This is used internal to the .class file to identify literals.
- • Otherwise, if the method intern has previously been called on an instance of class String containing a sequence of Unicode characters identical to that represented by the constant pool entry, then the result of resolution is a reference to that same instance of class String.
- • Otherwise, a new instance of class String is created containing the sequence of Unicode characters represented by the CONSTANT_String entry; that class instance is the result of resolution.