当前位置: 代码迷 >> J2SE >> String底层有关问题。
  详细解决方案

String底层有关问题。

热度:77   发布时间:2016-04-24 12:20:19.0
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吧。
  相关解决方案