Name: ###@###.### Date: 09/03/96
The section 3.10.5 String literals contains the following:
A string literal always refers to the same instance of class String.
...
Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5)
of class String (§4.3.3, §20.12). String objects have a constant value.
String literals-or, more generally, strings that are the values of constant
expressions (§15.27)-are "interned" so as to share unique instances, using
the method String.intern (§20.12.47).
Also the section contains the proper example, when the literal strings
within different classes in the same package represent references to
the same String object. The part of this example has been put into the
test below.
Test:
-----
package javasoft.sqe.tests.lang.lex047.lex04702;
import java.io.PrintStream;
class lex04702 {
public static void main (String args []) {
System.exit(run(args,System.out));
}
public static int run(String args[],PrintStream out) {
String hello = "Hello";
if (Other.hello == hello){
System.out.println("Other.hello == hello");
return 0;
}
else{
System.out.println("Other.hello != hello");
return 2;
}
}
}
class Other { static String hello = "Hello"; }
The test returns bad result, that is equal literal strings
within different classes in the same package represent
references to the different String objects on the current
Java compiler.
======================================================================
- duplicates
-
JDK-1266229 other
- Closed
-
JDK-1265923 other
- Closed