Question

For example I have a class Fruit. I create 2 instances:

Fruit apple = new Fruit("apple");
Fruit orange = new Fruit("orange");

The value of the 2 instances are not the same thus I am looking for the answer to be false. I override the .equals() method and wrote the following method to do the test:

@Override
public boolean equals(Object otherObject){
    if(otherObject instanceof Fruit){ 
        return true;
    }
    return false; 
}

    if(apple.equals(orange))
        System.out.println("true"); 
    else
        System.out.println("false");

The above method gives me the answer as true. From my understanding, this is a correct response since this simply tests if they both belongs to the same Class which is true.

But I can't get around to testing the values of the instances itself. Please advice. Thanks.

Était-ce utile?

La solution

You should have included your Fruit class, but here is one way

static class Fruit {
  private String name;

  public Fruit(String name) {
    this.name = name;
  }

  @Override
  public boolean equals(Object otherObject) {
    // check for reference equality.
    if (this == otherObject) {
      return true;
    }
    if (otherObject instanceof Fruit) {
      Fruit that = (Fruit) otherObject;
      // Check for name equality.
      return (name == null && that.name == null)
          || name.equals(that.name);
    }
    return false;
  }
}

public static void main(String[] args) {
  Fruit apple = new Fruit("apple");
  Fruit apple2 = new Fruit("apple");

  Fruit orange = new Fruit("orange");

  if (apple.equals(orange))
    System.out.println("true");
  else
    System.out.println("false");

  // You can also use the shorter
  System.out.println(apple.equals(apple2));
}

Outputs

false
true

Autres conseils

As Java uses implicit references you need to make a difference between object euqality and reference euqality.

If you just want to know wether two objects denode the same memory cell, so they are really the same, you cann use the equals operator:

Object A = new Fruit("A");
Object B = new Fruit("A");
System.out.println(A == B);

This would print false, as A and B are not the same reference cell.

Object A = new Fruit("A");
Object B = A
System.out.println(A == B);

Would return true, as they both are "pointers" to the same reference cell.

If you want to achieve semantic equality, I would advise you to make use of the equals method AND the fields of the class.

Object A = new Fruit("A");
Object B = new Fruit("A");
System.out.println(A.equals(B));

should return true in this case.

To achieve this you can usethe following equals method for every possible class you could write: (assuming you have getter and setter for the field A and your class is named myClass)

public boolean equals(Object B){
if(B instanceof MyClass){
  MyClass B = (MyClass) B;
  boolean flag = this.getA().equals(B.getA());
  return flag;
}
  return false;
}

You would have to do the boolean flag = this.getA().equals(B.getA()); for every field of the class. This would lead to equality if the objects fields have the same values.

But oyu have to keep in mind, that there is no perfect equals method. There is the so called hashcode equals contract in java which means that A.equals(B) has to hold, whenever A.hashCode()==B.hashCode()

A nice thing of the flag approach is that you don't have to care about the types of the objects fields, as for Basic Types that are not Objects (int, float, ...) the Java Runtime will do implicit bocing and cast them to Integer or Float Objects and use the equals method of them.

@Override
public boolean equals(Object otherObject){
    if(!otherObject instanceof Fruit){ 
        return false;
    }
  return ((Fruit)otherObject).getName().equals(name);
}

Let's say that "apple" is stored as the name of your Fruit (as a member String m_name for example)

Then in your equals method:

if( otherObj instanceof Fruit
 && (Fruit)otherObj.name.equals(this.name))
return true;
else 
return false;

How about this, simple and easy :

public boolean check(Object ObjectOne, Object ObjectTwo) {
    return ObjectOne.getClass() == ObjectTwo.getClass();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top