Overwriting Methods And Comparing Objects

I am having a hard time understanding why the method override below works since the == operator compares location in memory and the .equals() method compares references, and this means that the comparison made here should not be true because there are two different locations in memory and there are also two different objects, they are not comparing content.

Here is how the method override is used in the main class, and it outputs “true”

public static void main(String args){
var point1 = new Point(1,2);
var point2 = new Point(1,2);
System.out.println(point1.equals(point2));

Primitives (long, int, boolean, etc) and objects work differently with the == operator. For a primitive, it compares the literal values. For an object, it compares the memory address as you described (effectively answering: “is this the exact same object”). Hope that helps!

Thank you sir, but herein lies my confusion. These are not the same objects and therefore the outcome should be false. I see a redundancy in the code:

“return other.x == x && other.y== y;”

The logic alone in this code is forcing a “true” statement because “this.x” will always be “x” and “this.y” will always be “y”, so it does not allow for a real comparison of the objects as in:

“point1.equals(point2);”

So, can you please help me understand in more detail what am I missing here? Why would I want to force the code to output true and avoid a true comparison?

If “point1= point2;” then “point1.equals(point2);” and “point1 == point2;” would both make sense."

Let’s take a practical case:

Point first = new Point(1, 3);
Point second = new Point(2, 4);
Point third = new Point(1, 4);
Point fourth = new Point(1, 3);

Now let’s check how things go for each of the equals calls by expanding the expression other.x == x && other.y== y;

For first.equals(second):

// this = first, so x = 1, y = 3
other.x == 1 && other.y== 3;
// other = second
second.x == 1 && second.y== 1;
// second.x = 2, second.y = 4
2 == 1 && 4 == 3;
// Reduces to
false && false
// Reduces to
false

That is exactly what we expected.

For first.equals(third):

// this = first, so x = 1, y = 3
other.x == 1 && other.y== 3;
// other = third
third.x == 1 && third.y== 1;
// third.x = 1, third.y = 4
1 == 1 && 4 == 3;
// Reduces to
true && false
// Reduces to
false

Also what we expected.

For first.equals(fourth):

// this = first, so x = 1, y = 3
other.x == 1 && other.y== 3;
// other = fourth
fourth.x == 1 && fourth.y== 1;
// fourth.x = 1, fourth.y = 3
1 == 1 && 3 == 3;
// Reduces to
true && true
// Reduces to
true

Exactly what we wanted.

So I think you must have a misunderstanding about how the variable replacements work in the method. If it makes it easier to reason about you can prefix the member variables with this:

other.x == this.x && other.y== this.y;

Thank you, sir. I have been coding since 1 pm, 11 hours straight. And now, after reviewing your last response and reading your last line of code, and comparing it to my screen capture, I realized that I read “other.x” as “this.x”. How embarrassing, I was so confused. “This.x” is the predetermined field “x”, and “other.x” is the passed argument, and the code “return other.x == x && other.y == y” is testing that both coordinates of the Point objects and those of the arguments match. I need a rest and some wine.

Yes, definitely get some rest. The mind needs a break from time to time!

Related: Check out the Pomodoro technique if you want to stay productive while giving yourself rest breaks so you can be most effective.

1 Like