#Overriding .equals method

1 messages · Page 1 of 1 (latest)

primal glacier
#
public boolean equals(Object compared) {
        if (this == compared) return true;
        if (!(compared instanceof Person)) return false;

        Person comparedPerson = (Person) compared;
        return this.name.equals(comparedPerson.name);
    }

Hello, consider the code above. Can someone explain why we should first check if compared is an instance of Person before proceeding with the casting pls.

Also, can someone just review this piece of code pls, is it ok or it can made better?

knotty wedgeBOT
#

<@&987246399047479336> please have a look, thanks.

eager axle
#

The contract of equals requires that it return false, not throw a ClassCastException.

#
Integer a = 1;
String b = "x";
boolean r = a.equals(b); // false
primal glacier
#

yeah I tried to remove it and my IDE told me that the equals method required us to check the class type or something like that

primal glacier
eager axle
#

And in modern Java we'd write.

@Override
public boolean equals(Object o) {
    return o == this || o instanceof Person p
        && this.name.equals(p.name);
}
primal glacier
#

oh didn't know about this

#

wait will try to understand

#

o instanceof Person p

This part, normally, we check if something is an instance of something by writing compared instanceOf Something, right? Here what is the variable p acting as pls. I also noticed we also use an OR operator instead of and AND operator

eager axle
#

It's the equivalent of the instanceof Person test and declaring a variable p of that type.

primal glacier
#

yeah I see, will read a bit on that and come back

zenith timber
#

it would crash if you don't check the class

dire grail
merry viper
#

need to do a p.getClass() == this.getClass() too

#

(not p.getClass() == Person.class because then it’s a headache for anyone extending it)

dire grail
zenith timber
merry viper
primal glacier
#
public static double getPerimeter(Shape shape) throws IllegalArgumentException {
        if (shape instanceof Rectangle r) {
            return 2 * r.length() + 2 * r.width();
        } else if (shape instanceof Circle c) {
            return 2 * c.radius() * Math.PI;
        } else {
            throw new IllegalArgumentException("Unrecognized shape");
        }
    }

I'm currently reading the docs for the pattern matching expression. In the docs, part of it says this:

A pattern is a combination of a test, which is called a predicate; a target; and a set of local variables, which are called pattern variables:

The predicate is a Boolean-valued function with one argument; in this case, it’s the instanceof operator testing whether the Shape argument is a Rectangle or a Circle.

The target is the argument of the predicate, which is the Shape value.

The pattern variables are those that store data from the target only if the predicate returns true, which are the variables r and s.

Can someone explain the third part where the we talk about pattern variables pls. So if it's an instance of Rectangle for e.g, we create a variable named r of type Rectangle?

knotty wedgeBOT
cloud ledge
#
if (shape instanceof Rectangle) {
  Rectangle r = (Rectangle) shape;
 ... 
}
#

its essentially just short for that

primal glacier
#

yepp I see

#

I understand the pattern matching now but I guess I will have to use it for it to stick, thanks !