#Question about comparing equality of objects in java

1 messages · Page 1 of 1 (latest)

austere lake
#

Example from learning material:

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 int getDay() {
    return this.day;
}

public int getMonth() {
    return this.month;
}

public int getYear() {
    return this.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;
}

}

I need help with this part of code and what follows it : if (!(compared instanceof SimpleDate)) {
return false;
}
So if an object beeng compared with is not of type SimpleDate, return false. And then the next line of code is converting compared object into SimpleDate type object. But that line of code can not be executed if compared object type in not of SimpleDate type according to the condition above, which returns false (meaning it stops there). And if that condition is false, which means compared object is of SimpleDate type, what is the point of converting it to the SimpleDate type again? Am I missing something here

amber bluffBOT
#

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

silk belfry
#

If u reach that point in the code the object was always of type SimpleDate but you didnt know that until you performed the instanceof typecheck and so now you can safely type cast so you can access the methods and attributes of type SimpleDate

#

Otherwise you'll be limited to what Object gives you

#

Which is basically just hashcode and equals defaults

#

You couldnt cast it without an error if it was a different type*

#

*with the exception of some subclasses/interface implementations etc

austere lake
# silk belfry If u reach that point in the code the object was always of type SimpleDate but y...

Thanks for replay Xorium, but I am still a bit confused. So in that 'if' statement there is an exclamation mark ! which means if compared object is not an instance of SimpleDate (pythonic way 🙂 )--> return false. So to my understanding, 'compared' object is not of SimpleDate type and should be converted into that type, but program returns something if that evaluates to true (which is false in this case) and execution of the rest of that method will not occur, and that 'compared' object will never been cast into SimpleDate type object. Just makes no sense to me

silk belfry
#
if object is not SimpleDate:
  return False
else:
  date = (SimpleDate) object;
  return this.year == date.year and this.month == date.month and this.day == date.day

Amalgamated python and java

#

If it isnt a simple date it cant be equal

#

But if it is then we check the year month day

austere lake
bronze grove
#

let me make this a little bit clearer

silk belfry
#

Because its being polymorphically identified as an Object type and needs to be casted into the more specific type

bronze grove
#
    public boolean equals(Object compared) {
        return compared instanceof SimpleDate comparedSimpleDate &&
            this.day == comparedSimpleDate.day &&
            this.month == comparedSimpleDate.month &&
            this.year == comparedSimpleDate.year;
    }
#

the code you have is a wackier version of this

#

you just check "is it an instance of the same class, if so define comparedSimpleDate to be a SimpleDate object

#

then you check all the fields

austere lake
bronze grove
#

the == check on this is a probably not needed hypothetical perf enhancement, its not really related to correctness

#

because if they are literally the same object you don't need to do any checks

#

the (SimpleDate) compared; thing is the old way of doing this