views:

188

answers:

5

Assuming equals() is transitive; I understand that if x and y, have a bilateral agreement of being equal, then one of them, say y, does not enter into an agreement with a third class z on its own. But if we have a situation where x.equals(y) = false (still transitive) then what should this bilateral agreement with z be?

+2  A: 

Well:

x ≢ y
y ≡ z

since equals() is transitive, you can replace y with z:

x ≢ z

Therefore, x.equals(z) is false.

Edit: It just comes down to boolean logic, which is also transitive.

Mononofu
As you may or may not know, `x == y` and `x.equals(y)` are not equivalent in Java. The question is about the `equals(Object)` method, so in order to avoid confusion I wouldn't use the `==` operator in an answer, even as a shorthand notation for `equals(Object)`.
Bolo
I just used `==` and `!=` because they are the standard logic operators, and the question was whether `x` equaled to `z`. It doesn't really matter how equals() is implemented, since the fundamental rules are the same as for boolean logic.
Mononofu
In that case I would use `≡` and `≢`, or `⇔` and `⇎`. Maybe it's just me, but in the Java context it's very misleading to use `==` and `equals` interchangeably. Note: I'm not saying that your answer is wrong, but the answer may be confusing to novice Java programmers reading it.
Bolo
I'd also prefer those symbols, it's just that I didn't know how to type them. I'll guess I'll update my post accordingly.
Mononofu
A: 

There can be only two main scenarii:

  1. x.equals(y)

    y.equals(x)
    if x.equals(z) then y.equals(z), z.equals(x), z.equals(y)
    if !x.equals(z) then !y.equals(z), !z.equals(x), !z.equals(y)
    
  2. !x.equals(y)

    !y.equals(x)
    if x.equals(z) then !y.equals(z), z.equals(x), !z.equals(y)
    if !x.equals(z) then !z.equals(x) // you can't know the rest for sure
    
Colin Hebert
+1  A: 

@Colin HEBERT has answered this. I'd just like to point out a possible source of the OP's confusion.

There are actually two different relations here:

  • the EQ relation (i.e. x.equals(y) == true) is transitive.

  • the NE relation (i.e. x.equals(y) == false) is NOT transitive.

Furthermore, the transitivity property only allows you to reason about to chains involving one relation; i.e. x EQ y && y EQ Z implies x EQ Z. The question tries to use transitivity to reason about x NE y && y EQ z ... and it doesn't apply to that case.

Stephen C
just an example: using "is greater than" which also is transitive: `A > B` and `B > C` implies in `A > C`. But if we have `A <= B` and `B > C` we can NOT say `A <= C` or `A > C`, both are possible.
Carlos Heuberger
+4  A: 

If the equals method is correctly implemented, as demanded by the javadoc of Object:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

we can infer that x.equals(z) must be false.

Prove, if equals() is transitive and symmetric, x.equals(y) is false and y.equals(z) is true:

1) assuming x.equals(z) is true;
2) z.equals(y) is true (symmetry);
1+2) x.equals(y) is true (transitive 1 and 2)

but x.equals(y) is given as false, so number 1 or number 2 must be wrong, that is, x.equals(z) is false or the function is not symmetric.

But if equals() is not implemented to be symmetric, you can't say anything about the result x.equals(z) (see other answers; my comment on @Stephen C answer)

Carlos Heuberger
A: 

TO @Colin HEBERT: The assumption here is that equals() method does abide by the contract of equivalence relation. So for objects x and y if x.equals(y) returns false then y.equals(x) is also false. This is the symmetric relationship between them. Now for transitivity, if we have another object z and y.equals(z) is true. What should x.equals(z) return in context of this equivalence relation where x.equals(y) and y.equals(x) return false, and why?

msk