在阅读了有关 Java 字符串池的内容后,我决定进行一些测试。 我编写了以下测试程序,它的结果与 JDK 规范中的 Java String Pool 行为不匹配。
public class Temp_20141220 {
private String b1 = "hello";
public static void main(String[] args) {
Temp_20141220 t = new Temp_20141220();
String b = "hello";
System.out.println("b==b1 =" + t.b1 == b);
}
}
我一直期待得到输出:b==b1 =true 但我得到了:b==b1 =false
为什么会这样?难道 Ubuntu 的 JVM 不支持这个 Java String Pool 特性吗?
最佳答案
这个
System.out.println("b==b1 =" + t.b1 == b);
相当于
System.out.println( ("b==b1 =" + t.b1) == b);
您在 b
和 "b==b1 ="
和 t.b1
的串联之间应用引用相等性。
关于Java 字符串池测试不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680346/