0

Suppose, I have a Class, call it Student. The student class has an element, an int called Id. I want to override equals, so that if a Student is compared to an Integer the method returns true. like:

public class OverrideTest {
    public static void main(String[] args) {

        Student a = new Student();
        a.setId(5);
        System.out.println(a.equals(5));
    }

    public static class Student {

        private int id;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof Student) {
                Student a = (Student) o;
                if (a.getId() == this.getId())
                    return true;
            }
            if (o == this)
                return true;
            if (o instanceof Integer) {
                int id = (Integer) o;
                if (id == this.getId())
                    return true;
            }
            return false;
        }
    }

}

Is there a way to signal the IDE that this is okay and no warnings have to be sent?

This returns true, but with a syntax warning in IDE.

Nathan Hughes
  • 85,411
  • 19
  • 161
  • 250
Paul Woitaschek
  • 5,911
  • 5
  • 27
  • 49
  • I don't see any warnings.. – vefthym Nov 06 '14 at 18:37
  • I would recommend that you read [this question](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) and look for things to be aware when overriding equals. – Jonathan Drapeau Nov 06 '14 at 18:38
  • Warings come if you use a boolean equals = student.equals(5) for example. – Paul Woitaschek Nov 06 '14 at 18:38
  • 1
    That warning is most probably coming from your IDE rather than the JVM itself. – Jonathan Drapeau Nov 06 '14 at 18:40
  • 7
    This is _not_ okay, though. It breaks the contract of `equals`, which includes symmetry (`a.equals(b) == b.equals(a)`). There's no way you can get `Integer.equals` to agree with your definition, so your definition will be broken. – yshavit Nov 06 '14 at 18:40
  • Here's another [questions](http://stackoverflow.com/questions/113511/hash-code-implementation) to read for equals overriding. – Jonathan Drapeau Nov 06 '14 at 18:42

1 Answers1

5

As Yshavit noted, while you can do what you're trying to do, it's extremely dangerous because it breaks the symmetry of the .equals(...) relation. For example, consider:

Student s = new Student(5);
Integer i = new Integer(5);
if(s.equals(i)) System.out.println("s equals i");
else System.out.println("s doesnt equal i");
if(i.equals(s)) System.out.println("i equals s");
else System.out.println("i doesnt equal s");

Output:

s equals i
i doesnt equal s

Logically this doesn't make any sense. There is probably a way to convince your IDE to ignore the warning here, but you should almost certainly heed it and not make Students able to equal Integers.

The equals(..) method is used internally in containers such as HashSets, so this method would cause have erratic behavior in built-in structures.

More about overriding equals(...)

Community
  • 1
  • 1
Mshnik
  • 6,944
  • 1
  • 21
  • 37