About override the equals method

Category: Java SE
2011-01-20 09:50:08

Sponsored Links
class Person {
private String name;
private String location;

Person (String name) {
this.name = name;
location = "haerbin";

Person (String name, String location) {
this . name = name;
this.location = location;

public String Info () {
return "name:" + name + "," + "location:" + location;


class Teacher extends Person {
private String job;

Teacher (String n, String j) {
this (n, "beijing", j );

Teacher (String name, String location, String job) {
super (name, location);
this.job ; = job;

public String Info () {
return super.Info () + "," + "job : "+ job;

public class TestE {
public static void main (String [] args) {
Person p1 = new Person ("leisao");
Person p2 = new Person ("dachao", "jilin");
Teacher t1 = new Teacher ("jinfeng", "gongwuyuan");
Teacher t2 = new Teacher ("xiaogang", "yunnan" , "yazi");
Person p3 = new Person ("leisao");
Person p4 = new Person ("leisao");
System.out.println (p3.equals (p4));
System.out.println (p1.Info ());
System.out.println (p2.Info ()) ;
System.out.println (t1.Info ());
System.out.println (t2.Info ());

Why p3 and p4 print results will be false comparison does not mean that the same sort string comparison as long as you can Well
like a long time, thought out, tested several override the equals method, also wrote bad
seeking expert can help to write the equals method that allows comparison p3 and p4 print out the true

Sponsored Links

2011-01-20 10:20:38
Why p3 and p4 print results will be false comparison does not mean that as long as the string comparison Well the same can be sorted

A: p3 and p4 are not strings. They are the Person object.

 * @author bzwm
class Person {
private String name;

private String location;

Person(String name) {
this.name = name;
location = "haerbin";

 * @return the name
public String getName() { //
return name;

 * @param name the name to set
public void setName(String name) {//

this.name = name;

Person(String name, String location) {
this.name = name;
this.location = location;

public String Info() {
return "name:" + name + "," + "location:" + location;

public boolean equals(Object o){//
if(!(o instanceof Person)){
return false;
return name.equals(((Person)o).getName());

class Teacher extends Person {
private String job;

Teacher(String n, String j) {
this(n, "beijing", j);

Teacher(String name, String location, String job) {
super(name, location);
this.job = job;

public String Info() {
return super.Info() + "," + "job:" + job;

public class TestE {
public static void main(String[] args) {
Person p1 = new Person("leisao");
Person p2 = new Person("dachao", "jilin");
Teacher t1 = new Teacher("jinfeng", "gongwuyuan");
Teacher t2 = new Teacher("xiaogang", "yunnan", "yazi");
Person p3 = new Person("leisao");
Person p4 = new Person("leisao");
2011-01-20 10:59:01
equals is a reference comparison, which is relatively address, p3 and p4 are not the same object, different addresses
override equals Compare the Person object is the same on all domains

find online information, I hope to help you
override equals () method
one, specification
Java language specification requires the equals method has the following characteristics:
1) is reflexive: for any non-null reference x, x.equals (x) should return true.

2) symmetry: For any references x and y, and if x.equals (y) returns true, then

what y.equals (x) should also be return true.

3) transitive: for any reference to x, y and z, if x.equals (y) returns

true, y.equals (z) returns true , then x.equals (z) should return true.

4) Consistency: If x and y referenced object does not change, then repeatedly call

x.equals (y) should return the same result.

5) For any non-null reference x, x.equals (null) should return false.

two recommended
given below equals method to write a perfect recommendation:

1) explicit parameter named otherObject, later want to It is converted into another called

do other variables.

2) detects this and otherObject refer to the same object:

if (this ; == otherObject) return true;

This statement is just an optimization. In fact, this is a frequently used form of

type. Because the calculation of the equation than one class in a domain to compare

paid a price much smaller.

3) detecting whether otherObject is null, if null, returns false. This

detection is necessary.

if (otherObject == null ) return false;

compare this with otherObject belong to the same class. If the language of equals

defined in each subclass change, use getClass detection:

; if (getClass ()! = otherObject.getClass ()) return ; false;

If all subclasses have a unified semantics, use instanceof test:

; if (! (otherObject instanceof ClassName)) retrun false;

4) will be converted to the corresponding otherObject class type variables:

ClassName other = (ClassName ) otherObject;

5) now need to compare all of the domains compared. Use == compares

basic type field, using the equals comparison object domain. If all the fields are matched with

, it returns ture; otherwise false.

return field1 == other.field1

; && field2.equals (other.field2)

; && ......;
2011-01-20 11:28:21
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;

public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;

be provided hashcode ()
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.