字符串比较之 “==”和 “equals”
提示:引用存放在栈里,实体存放在堆里。
String a = "String A";
String b = new String("String B");
String c = a;
String d = new String("String B");
请看堆栈示例图:
fuck ,为什么图片不能粘贴呢。鄙视iteye.附件里面有。
//打印hashcode值:
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(c.hashCode());
System.out.println(d.hashCode());
//打印结果
System.out.println(a==c);
System.out.println(b==d);
System.out.println(a.equals(c));
System.out.println(b.equals(d));
Result:
1859651602
1859651603
1859651602
1859651603
true //a==c
false//b!=d
true//a.equals(c)
true//b.equals(d)
本文探讨了Java中字符串比较的方法,重点介绍了使用“==”与equals()的区别。通过具体实例展示了不同情况下两者的行为差异,并提供了hashcode值的对比,加深读者对字符串存储位置的理解。
1025

被折叠的 条评论
为什么被折叠?



