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 different packages represent references to
the same String object. The part of this example has been put into the
test below.
Test consist of two files which are compiled jointly:
-----------------------------------------------------
File lex04703:
--------------
package javasoft.sqe.tests.lang.lex047.lex04703;
import java.io.PrintStream;
class lex04703 {
public static void main (String args []) {
System.exit(run(args,System.out));
}
public static int run(String args[],PrintStream out) {
String hello = "Hello";
if (javasoft.sqe.tests.lang.lex047.lex04703.lex04703a.lex04703a.hello == hello) {
System.out.println("lex04703a.hello == hello");
return 0;
}
else {
System.out.println("lex04703a.hello != hello");
return 2;
}
}
}
File lex04703a:
--------------
package javasoft.sqe.tests.lang.lex047.lex04703.lex04703a;
public class lex04703a { public static String hello = "Hello"; }
The test returns bad result, that is equal literal strings
within different classes in different packages represent
references to the different String objects on the current
Java compiler.
======================================================================
- duplicates
-
JDK-1265923 other
- Closed