[英]Java string pool test doesn't work
After reading about Java String Pool I've decided to run a little test. 在阅读了有关Java字符串池的信息后,我决定进行一些测试。 I've written the following test program that it's result doesn't match the Java String Pool behavior according to the JDK specification. 我编写了以下测试程序,根据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);
}
}
I've been expecting to get in output: b==b1 =true but I got: b==b1 =false 我一直期望得到输出:b == b1 = true,但我得到:b == b1 = false
Why so? 为什么这样? Does Ubuntu's JVM not support this Java String Pool feature? Ubuntu的JVM不支持此Java字符串池功能吗?
This 这个
System.out.println("b==b1 =" + t.b1 == b);
is equivalent to 相当于
System.out.println( ("b==b1 =" + t.b1) == b);
You're applying reference equality between b
and the concatenation of "b==b1 ="
and t.b1
. 您将在b
与"b==b1 ="
和t.b1
的串联之间应用引用相等。
you're comparing 2 different strings: 您正在比较2个不同的字符串:
"b==b1 =" + t.b1
字符串是"b==b1 =" + t.b1
if you change your code to: 如果将代码更改为:
System.out.println("b==b1 =" + (t.b1 == b));
you will get true in output 您将在输出中实现真实
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.