equals是比对两个对象是否相同,而==则是看两个字符串是否为同一内存地址。new String("");给变量附值时在内存中其实是重新开辟了一快空间放这个新的字符串。如:
String str1 = new String("hello");
String str2 = str1; //将str1附给一个新的String,它们的内存地址是相同的
String str3 = new String("hello");用equals来比对这三个字符串,结果都会为true
用==的话str1和str2为true,str1和str3为false,str2和str3为false<------ 树欲静而风不止 ------>
String str1 = new String("hello");
String str2 = str1; //将str1附给一个新的String,它们的内存地址是相同的
String str3 = new String("hello");用equals来比对这三个字符串,结果都会为true
用==的话str1和str2为true,str1和str3为false,str2和str3为false<------ 树欲静而风不止 ------>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货