String初始化问题
String k = "abc ";
String o = "a ";
String p = "bc ";
String m = "a " + "bc ";
String n = o + p;
System.out.println(k == m);
System.out.println(k == n);
运行结果为:
true
false
请前辈解析一下下
------解决方案--------------------http://community.csdn.net/Expert/topic/5665/5665749.xml?temp=.8930933
------解决方案--------------------String m = "a " + "bc ";//这个编译器已经清楚知道它的计算结果,所以直接变成m = "abc "
而在字符串常量是共享的,所以k == m
但是String n = o + p;编译器并不肯定它的计算结果,所以它不会优化,所以等同于
n = new StringBuffer().append(o).append(p).toString();
所以 k != n