日期:2014-05-20  浏览次数:20617 次

String底层问题。。。
 
  String s1 = "Hello World";
  String s2 = new String("Hello World");

  System.out.println(s1 == s2);

 为什么会输出false?

能告诉我底层原理吗?

------解决方案--------------------
最经典的月经贴!
------解决方案--------------------
大姨妈又要来了。。。。等号比较的是内存位置,引用的对象所在的内存完全一样才为true.
------解决方案--------------------
== 是比较内存地址的。 
"Hello World" 对象是放在常量池中。
new String("Hello World"); 实例化一个java对象,在堆中新开一块内存。


String s1 = "Hello World";
String s2 = "Hello World";

这样子时true吧。