规范
Java语言规范第8版中的例子3.10.5-1.字符串字面量告诉我们:
由编译单元(§7.3)组成的程序:
package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }
编译单元:
package other;
public class Other { public static String hello = "Hello"; }
产生输出:
true true true true false true
现实情况
但是使用 Oracle JDK 1.8.0_65(Windows)进行编译和运行会产生以下输出:
true true true true true true
问题 1
这有什么不同? (我有一个猜测,并将发布答案)
问题 2
这是规范、编译器还是解释器的错误吗?
如果是其中之一,应该在哪里报告?
版本
"c:\Program Files\Java\jdk1.8.0_65\bin\java.exe" -version
java version "1.8.0_65"
Java(TM) SE Runtime Environment (build 1.8.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
"c:\Program Files\Java\jdk1.8.0_65\bin\javac.exe" -version
javac 1.8.0_65
JIT
是聪明的,但他的兄弟编译器也并非完全愚蠢。 - Kayaman