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

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.)
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.