Casting in equals method

梦想的初衷 提交于 2019-12-03 17:20:43

问题


I have a question about overriding the equals method in Java. In my book, I have the following example:

public class Dog{
     private String name;
     private int age;

public boolean equals(Object obj) {
    if(!(obj instanceof Dog)) return false;
    Dog other = (Dog) obj;                    ---> confused here
    if(this.name.equals(other.name) && (this.age == other.age) {
     return true;
    }
    else {
     return false;
    }
  }
}

I don't understand why why have to cast the reference to the Dog reference. If that reference is not of type Dog we return false. Why all the hassle with casting it ?


回答1:


The declared type of obj is Object, so you must cast it to tell the compiler that it is a Dog.

Although logically it can't be anything else at that point in the code, the compiler doesn't know anything about logic - it only knows about the type.




回答2:


Because you are defining your own parameters for equality, you have to make sure they are the same class. That is, unless you're comparing them the == way, then you need to compare some value inside the objects. To compare values inside the objects, they need to be the same type!

For example, let's say you have two Dogs.

Dog dog1 = new Dog("Fido");
Dog dog2 = new Dog("Rover");

If you want to test if they have the same name, as I'm sure you know, you can't use:

if(dog1 == dog2)

So you override the equals method. However, because you're overriding it, it has to have the same method signature. A method signature is defined by the name of the method, and the number and type of it's parameters. Which means if you wish to override it, it needs to have a parameter of type Object. Hence:

if(dog1.equals(dog2))

The reason you need to cast it to use whatever method you're using to get the name value from the dog, and compare those values.

A note on your class design

The convention in object oriented programming, and certainly in Java, is to have Accessor and Mutator methods to get and change variables in a class. That is:

dog1.name; ----> dog1.getName();

where getName() looks like:

public String getName()
{
    return name;
}



回答3:


You are overriding equals method to check equality by value not by reference..

i.e You want two dogs equal if they have same name and age not if they are owned by the same person(i.e not if their references are equal)


Without cast you can't access dog's name and age members




回答4:


For JVM it is not that obvious that obj is Dog, so you have to be explicit with the cast.




回答5:


Because as method parameter obj is Instance of Object class. So in the method body you need to cast obj back to the Dog type.




回答6:


The properties name and age are specific only to Dog. You can't access name or age by using obj, which is an Object. For ex, the following code will generate compile time error:

this.name.equals(obj.name)

I don't understand why why have to cast the reference to the Dog reference. If that reference is not of type Dog we return false. Why all the hassle with casting it ?

Because although the reference is of Dog it doesn't means that it is the same Dog. The name of the other dog may not be the same as your Dog. In order to compare the name and age of the other Dog with your Dog, it has to be cast first.




回答7:


I think that your demo class is missing a hashCode() method. The two objects can only be equal if their call to an overridden hashCode() is equal. (the same integer value).

I'm pretty sure you need both to guarantee this equation.



来源:https://stackoverflow.com/questions/16497471/casting-in-equals-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!