Question

I need to make equals function for MyClas.

public class MyClass
{
boolean equals(Object value)
  {
    if (... value is type of MyCLass ...)
      {
        return= ... check conditions...;
      } else return false;
  }
}

For this purpose I need to know if value of Object is type of MyClass. How to make it?

Était-ce utile?

La solution

In order to check if value is of type MyClass use:

 if( value instanceof MyClass) 

Autres conseils

instanceof operator is used to determine that. It's infix, so use it like so...

(value instanceof MyClass)
    public class MyClass
    {
       boolean equals(Object value)
      {
           if (value instanceof  MyCLass)
           {
              return= ... check conditions...;
           } else return false;
       }
   }

You can do

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    MyClass myClass = (MyClass) o;
    //Your logic

You can also use instanceof instead of getClass() approach.

Just a little IDE trick. Just to save up some time.

In eclipse you can do that by right click on the class file and select source --->generate hashCode() and equals() method , select all the attribute you need to compare and IDE will generate corresponding code for you

An excerpt

public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        if (id != other.id)
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        if (salary != other.salary)
            return false;
        return true;
    }
value instanceof ClassName

the instanceof key word checks, where value is a subclass of ClassName, If yes reutns true and otherwise returns false

Altough RTTI (Real Time Type Identification) is considered a code smell by some individuals, there are two alternatives, one is to use the instanceof operator:

if(value instanceof MyClass)

On the other hand, you can use a full fledged method from the Class class, that given two objects, you can determine if they belong to the same hierarchy (much more powerful than instanceof IMO):

if(value.getClass().isAsignableFrom(getClass()))

This second approach determines if value is the very same class or a superclass/superinterface of the current class (the this) at execution time given any kind of object. This is where isAsignableFrom excels, since with instanceof you need to know the reference type at compile time.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top