0

hopefully this doesn't make me seem to be an idiot but I seem to be failing on a simple exercise where I have to compare two objects to check if they are equal, my Java class is below along with the error message I'm getting from the exercise. Would anyone know how to solve it? Thanks in advance.

import java.util.Objects;

public class Person {

    private String name;
    private SimpleDate birthday;
    private int height;
    private int weight;

    public Person(String name, SimpleDate birthday, int height, int weight) {
        this.name = name;
        this.birthday = birthday;
        this.height = height;
        this.weight = weight;
        hashCode();
    }
    public String getName(){
        return this.name;
    }
    public SimpleDate getBirthday(){
        return this.birthday;
    }
    public Integer getHeight(){
        return this.height;
    }
    public Integer getWeight(){
        return this.weight;
    }
    // implement an equals method here for checking the equality of objects
    @Override
    public boolean equals(Object compared){
        return this==compared;
    }
}

Error message

Cat
  • 3
  • 1
  • You call return null but better option is use `Optional` – Eklavya May 09 '20 at 07:52
  • 1
    Does this answer your question? [Compare two objects with .equals() and == operator](https://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator) – Progman May 09 '20 at 09:00

2 Answers2

4

Joshua Bloch in Effective Java gives guidelines on how to write a nice .equals(). Here's the excerpt directly from the book:

  1. Use the == operator to check if the argument is a reference to this object.

  2. Use the instanceof operator to check if the argument has the correct type.

  3. Cast the argument to the correct type.

  4. For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object.

  5. When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent?

    public boolean equals(Object o) {
        if(o == this) {
            return true;
        }
        if(!(o instance of Person)) {
            return false;
        }
       //you comparing logic here
    }
    

You have to make sure that equals follows its contract (it's an equivalence relation). See it's documentation for more details. Also, override the hashcode() method.

You equals method is written wrongly as it just compares the location of objects in memory. That's why your tests are failing.

Prashant Pandey
  • 4,332
  • 3
  • 26
  • 44
  • Ah I see, is there no way to somehow condense checking each field to be equal aside from doing it individually? (e.g fieldA==fieldA&&fieldB==fieldB&&... etc.) – Cat May 09 '20 at 09:21
  • Ideally, you need to compare each field. But what fields you want to compare, totally depend of the business. For instance, you might not even care about the weight of the person while comparing. – Prashant Pandey May 09 '20 at 09:26
0

You changed behaviour of equals to == here:

@Override
 public boolean equals(Object compared){
        return this==compared;
 }

and now here is already an answer - https://stackoverflow.com/a/13387787/7505731

Hemant
  • 1,403
  • 2
  • 11
  • 21