标签:
1 根类object中的equals()与==没有区别:
如普通类Person中:
Person per1 = new Person("张三",20);
Person per2 = new Person("张三",20);
System.out.print(per1.equals(per2));的结果为true。
2 String类改写了Object的equals();
==是指对内存地址的值进行比较 equals()是对字符串的内容进行比较
如:String str1="hello";
String str2=new String("hello");
String str3=new String("hello").intern();
System.out.print(str1.equals(str2));的结果为true,因为内容相同;
System.out.print(str1==str2);的结果为false,因为str1为引用类型;"hello"会直接进入对象池(堆内存)中,而str2中的不会入池。二者的地 址当然不一样。
而经手动入池的str3保证了其地址的值相同故:
System.out.print(str1==str3);的结果为true。
标签:
原文地址:http://www.cnblogs.com/yangchuan120120/p/4384635.html