String s0=”apple”;
String s1=”apple”;
String s2=”app” + “le”;
System.out.println( s0==s1 );
System.out.println( s0==s2 );
输出
true
true
双等号比较的是两个对象的引用地址,对于String创建的常量都放到了常量池中,s0、s1、s2存的是指向常量池里面值的引用地址。
String s0=”apple”;
String s1=new String(”apple”);
String s2=”app” + new String(“le”);
System.out.println( s0==s1 );
System.out.println( s0==s2 );
System.out.println( s1==s2 );
false
false
false
用new String() 创建的字符串不是常量,不在编译期确定,创建的字符串不放入常量池中,它们有自己的地址空间。 s0在编译时创建的对象存的是常量池中”kvill”的引用,s1运行时创建的新对象”kvill”的引用,s2也是运行时新创建对象”kvill”的引用。
String s0= “apple”;
String s1=new St