Equality. What does it mean for two objects to be equal? If we test equality with (a == b) where a and b are reference variables of the same type, we are testing whether they have the same identity: whether the references are equal. Typical clients would rather be able to test whether the data-type values (object state) are the same. Every Java type inherits the method equals() from Object. Java provides natural implementations both for standard types such as Integer, Double, and String and for more complicated types such as java.io.File and java.net.URL. When we define our own data types we need to override equals(). Java's convention is that equals() must be an equivalence relation:
Shamelessly copied from : http://algs4.cs.princeton.edu/12oop/. This is just being used as notes for revision purposes.
- Reflexive: x.equals(x) is true.
- Symmetric: x.equals(y) is true if and only if y.equals(x) is true.
- Transitive: if x.equals(y) and y.equals(z) are true, then so is x.equals(z).
- Consistent: multiple invocations of x.equals(y) consistently return the same value, provided neither object is modified.
- Not null: x.equals(null) returns false.
Shamelessly copied from : http://algs4.cs.princeton.edu/12oop/. This is just being used as notes for revision purposes.
No comments:
Post a Comment