# equals 和 ==, equals 和 hashcode

public class Person {
public String name;
public int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}
}

## 1. equals 和 ==

1. equals 默认就是 == ，比较引用内存地址
2. String、Integer 等类重写了 equals 方法, 比较值

Object.equals 源码

public boolean equals(Object obj) {
return (this == obj);
}

String.equals 源码

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}

 @Test
public void StringEquals() {
String a = "123";
String b = new String("123");
assertEquals(a, b);
assertTrue(a.equals(b));
assertFalse(a == b); // 涉及到常量池
}

### 重写 equlas 方法

@Override
public boolean equals(Object o) {
// 相同的引用地址
if (this == o) return true;

//  不是本类的实例
if (o == null || getClass() != o.getClass()) return false;

Person person = (Person) o;
// 比较具体值
return age == person.age && name.equals(person.name);

}

// 对于对象，重写 equals 使之可以进行值的比较
@Test
public void ObjectEquals() {
Person a = new Person("hehe", 10);
Person b = new Person("hehe", 10);
// 默认比较引用地址 false
//        assertNotEquals(a, b);
assertEquals(a, b);
}

## 2. equals 和 hashcode

 /**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
*     an execution of a Java application, the {@code hashCode} method
*     must consistently return the same integer, provided no information
*     used in {@code equals} comparisons on the object is modified.
*     This integer need not remain consistent from one execution of an
*     application to another execution of the same application.
* <li>If two objects are equal according to the {@code equals(Object)}
*     method, then calling the {@code hashCode} method on each of
*     the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
*     according to the {@link java.lang.Object#equals(java.lang.Object)}
*     method, then calling the {@code hashCode} method on each of the
*     two objects must produce distinct integer results.  However, the
*     programmer should be aware that producing distinct integer results
*     for unequal objects may improve the performance of hash tables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
*
* @return  a hash code value for this object.
* @see     java.lang.Object#equals(java.lang.Object)
* @see     java.lang.System#identityHashCode
*/
public native int hashCode();

1. 程序执行过程中，对同一对象多次调用 hashcode 应当返回一直，前提是将对象进行 equals 比较时所用的信息没有被修改。
2. 如果俩对象 equals, 则 hashCode 也应该相等
3. 如果俩对象不 equals，则不要求 hashcode 相等。但程序员们应当知道，为不相等的对象生成不同整数结果可以提高哈希表的性能。

• 本文已收录于以下专栏：

举报原因： 您举报文章：equals 和 ==, equals 和 hashcode 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)