1

First time posting here in a while, so I am very sorry if I made any mistakes.

I am learning java from MOOC fi. I am currently in week 5 and it mentions here that to compare two objects, you have to do the following:

public class SimpleDate {
    private int day;
    private int month;
    private int year;

    public SimpleDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public boolean equals(Object compared) {
        // if the variables are located in the same position, they are equal
        if (this == compared) {
            return true;
        }

        // if the type of the compared object is not SimpleDate, the objects are not equal
        if (!(compared instanceof SimpleDate)) {
            return false;
        }

        // convert the Object type compared object
        // into a SimpleDate type object called comparedSimpleDate
        SimpleDate comparedSimpleDate = (SimpleDate) compared;

        // if the values of the object variables are the same, the objects are equal
        if (this.day == comparedSimpleDate.day &&
            this.month == comparedSimpleDate.month &&
            this.year == comparedSimpleDate.year) {
            return true;
        }

        // otherwise the objects are not equal
        return false;
    }

    @Override
    public String toString() {
        return this.day + "." + this.month + "." + this.year;
    }
}

So I am confused, what does:

public boolean equals(Object compared) {
    // if the variables are located in the same position, they are equal
    if (this == compared) {
        return true;
    }

the above code actually do? What does located in same position mean?

Isn't it better to just check if an object is of a particular class type and then compare their instance variables to see if they are equal?

user207421
  • 305,947
  • 44
  • 307
  • 483
Praveen KG
  • 21
  • 4
  • 3
    `==` is comparing it's memory reference, so if both instances are pointing to the same location in memory, they have to be equal, otherwise it will compare the properties to ascertain if the two objects are otherwise comparably the same – MadProgrammer Oct 14 '21 at 04:29
  • 1
    Interesting tidbit: this specific `if` check and return is just an optimization: if you left it out, the code would compare all the values and still return `true` (assuming the rest of the `equals` method is written correctly). – Generous Badger Oct 14 '21 at 08:21

2 Answers2

1

this and compared are both references to objects in your case.

In Java each object resides at a particular memory location. Two different objects can NOT have the same memory location at the same time.

The equality operator or "==" compares two objects based on whether they are located at the same memory address. So "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.

Also note that == operator is not the same as the equals method on the class.

You can read more at: https://javarevisited.blogspot.com/2012/12/difference-between-equals-method-and-equality-operator-java.html#ixzz79Ev3iZ5P

Neeraj
  • 137
  • 1
  • 2
  • 9
1

== : compare memory address

.equals() : compare values(use for Object type)

Random Guy
  • 46
  • 4