Java ==,equals() 和hashCode

Kruger上课讲到==和equals()方法是不同的,经过查询将具体内容整理一下,在查询过程中发现hashCode()方法与equlas()联系紧密,故一起研究。

比较浅显,以后如果理解更多随时更新。

以下一段代码主要解释==与equals的不同

String a = "nihao";

String b = "nihao";

String d = "niha";

String e = new String("nihao");

String f = new String("nihao");

System.out.println(a==b);//true

System.out.println(a==d);//false

System.out.println(a==e);//false

System.out.println(a.equals(e));//true

System.out.println(e==e);//true

System.out.println(e==f);//false

System.out.println(e.equals(f))//true

 

1. ==

== is a reference comparsion. Both objects point to the same memory location.

而且==用于基本类型,比如int,float等,比较的是值。

用于对象类型,==比较的是两个对象在JVM的地址。比如a和b,两者都是字符串“nihao”的引用,指向同一个地址。(与String是不可变类有关,其实a,b是两个相同的instance,见immutable class的解释)。a和d不同字符串的引用,地址不同。

new出来的对象地址存在于堆上,所以e与a地址不同。同理,e==f也是错误的。

2. equals()

equals will only compare what it is writtern to compare. If a class does not override the equals methods, it defaults to the equals(object o) methods(从最近的父类继承而来的)。如果都没有override,那么equals从根类Object中继承而来。默认的此方法也是比较对象地址。

但是有一些子类已经override这个方法,比如String。在这个类中,equals首先比较对象的JVM地址,如果是同一个对象的引用(e==e),那么对象相等,返回true。如果不是一个对象(比如a和e),equals会挨个比较字符串对象内的字符,完全一致返回true。具体可以查询equals方法的源代码。

这就可以解释为什么对于String类型,比较需要用equals。

以此类推Double、Inteter、Math,都是override equals(),比较的都是内容。

需要注意的是,equals是进行的类以及类内数值的比较,请不要用equals直接比较两个数值,比如:

double a =1;

double b = 1;

System.out.prinln(a.equals(b)); 很明显,这个statement就是错的。

Java语言对equals()有如下要求:
a:对称性:如果x.equals(y)返回是“true”,那么y.equals(x)也应该返回是“true”。 
b:反射性:x.equals(x)必须返回是“true”。 
c:类推性:如果x.equals(y)返回是“true”,而且y.equals(z)返回是“true”,那么z.equals(x)也应该返回是“true”。 
d:一致性:如果x.equals(y)返回是“true”,只要x和y内容一直不变,不管你重复x.equals(y)多少次,返回都是“true”。 
e:任何情况下,x.equals(null),永远返回是“false”;x.equals(和x不同类型的对象)永远返回是“false”。 


3. hashCode()

Always remember to override hashCode if you override equals so as not to "break the constract". As per the API, the result returned from the hashCode() mehod for two objects must be the same if their equals methods shows that they ar equivalent. The converse is not necessairly.

hashCode也是根类Object中的方法,默认情况下返回对象的32为jvm内存地址。

下面来解释上面一段英文:

constract:hashCode方法的常规协定,标明相等对象必须具有相等的哈希码。(什么是哈希码?)因为哈希码通过hashCode检索对象。

如果override了equals方法,一定要override hashCode方法,如果不重写,Object对象中的hashcode方法始终返回的是一个对象的hash地址,而这个地址永远不相等,所以即使重写了equals,也不会有特定的效果。因为hashcode不相等的话就不会调用equals方法

原文地址:https://www.cnblogs.com/amberdata/p/5322672.html