字符串之间的比较


public class StringBiJiao{
public static void main(String[] args){
String s1="hello";
String s2="hello";
String s3=s1+"world";
System.out.println(s1.equals(s2));//ture比较内容,内容相同
System.out.println(s1==s2);//ture比较内存地址,变量内存中已经有hello,s2直接指向该内存地址,故s1s2内存地址相同
String ns1=new String("hello");
String ns2=new String("hello");
System.out.println(s1.equals(ns1));//ture
System.out.println(ns1.equals(ns2));//ture
System.out.println(ns1==ns2);//false,new的对象都是在堆内存,不管内容重复不,都会用新的内存
}
}

猜你喜欢

转载自blog.csdn.net/u011381797/article/details/80303702