programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Paul Clapham
• Devaka Cooray
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• Frits Walraven
Bartenders:
• Carey Brown
• salvin francis
• Claude Moore

# equals method

Ranch Hand
Posts: 61
i have the following snippet of code of the certification bokk SCJP

(a) return a == o.a || b == o.b;
(b) return false;
(c) return a >= o.a;
(d) return a == o.a;
(e) return a == o.a && b == o.b;
i don't undrestand why these answers are correct and why a+c are not

Marshal
Posts: 64179
215
Try solution a with the following three objects:- 1: new Pair(123, 234), 2: new Pair(234, 345), 3: new Pair(123, 345)
You will get true if you compare 1 and 3 or 2 and 3, but you won't manage to prove equality between 1 and 2, so it fails transitivity.

Ranch Hand
Posts: 3218
19
Campbell, thanks for clearing the posting problem.  I wrote this post a while ago; part is now redundant with your post, but I'll just post it as is anyway.

----

First, I would argue that none of these implementations are correct, except b.  Because they don't override hashCode() at the same time they override equals(), they create an equals() method which is incompatible with hashCode().  Except for b, only because this implementation is effectively the same as the default implementation inherited from Object.

I'm guessing the question was not intended to consider this, so let's pretend that is not an issue.

emma roberts wrote:the correct answer is b+c+d
i don't undrestand why these answers are correct and why a+c are not

I don't know what the book says, but I would say B, D, and E are correct.  Not A, not C.

Why not A?  Consider the following test case.  What happens, and does it make sense?

----

Why not C?  Consider this test, with the same questions:

emma roberts
Ranch Hand
Posts: 61
sorry but i also stay don't undrestand

Campbell Ritchie
Marshal
Posts: 64179
215

Mike Simmons wrote:. . . they don't override hashCode() at the same time they override equals(), . . . let's pretend that is not an issue.

I resisted the temptation to mention hashCode() yesterday, pretending that is not an issue.

. . .

emma roberts wrote:the correct answer is b+c+d . . .

. . .

She actually said BDE and I missed that she didn't understand C. Sorry.

To elaborate from what MS said, the equals() method has eight requirements. There might only be five •s printed in the documentation, but there are eight requirements:-
• A: You must override the method, not overload it. That isn't well put in the documentation.
• B: It is usually necessary to override hashCode() too. We are pretending that is not an issue, as the book seems to; let's say that is more a requirement of hashCode()'s.
• C: Bullet 1: Reflexivity: Easy: every object is equal to itself.
• D: Bullet 2: Symmetry: you must get the same result from x.equals(y) as you do from y.equals(x).
• E: Bullet 3: Transitivity. If x.equals(y) and y.equals(z), then you must get true from x.equals(z).
• F: Bullet 4: Easy: You must get the same result if you use the method twice on the same data.
• G: Bullet 4½: This is the one you are likely to miss: If you always get the same result, your method mustn't throw any exceptions.
• H: Bullet 5: If you pass null as an argument it returns false. An object which exists is different from an object which doesn't exist.
• Note G. If you call a method on a null reference, you get an exception, but it is thrown before the equals() method is called at all; the method itself mustn't allow any exceptions. The == operator and the instanceof operator in line 10 will ensure nulls are caught without throwing any exceptions.Answer C: If you have a Pair with different values for a the >= operator will allow it to pass one way and not the other. Run MS' second example. You will find it violates symmetry: you get true one way round and false the other way.
Answer A: If you run MS' first example or mine, you will see you get true twice, in which case you should get true from the third test, but you actually get false.

Theequals() method is fundamental to the use of the Collections Framework and lots of other programming, and it is notoriously difficult to get right. You therefore need to know about it whether you are sitting an exam or not. I shall therefore duplicate this discussion in a non‑exam forum. I can think of three good resources about equals():-
• Joshua Bloch's Effective Java. Somewhere in chapter 3.
• Odersky, Spoon and Venners
• Angelika Langer
• I think it is worth reading all three of them; they say the same thing, more or less, but in different ways.

Mike Simmons
Ranch Hand
Posts: 3218
19

Campbell Ritchie wrote:

Mike Simmons wrote:

emma roberts wrote:the correct answer is b+c+d . . .

. . .

She actually said BDE and I missed that she didn't understand C. Sorry.

No, I quoted the original version.   It was edited later.  My actual post came even later, because I had been unable to reply initially, and I didn't notice that she'd fixed the original post at that point.

Mike Simmons
Ranch Hand
Posts: 3218
19
Emma, if this is still unclear, I suggest compiling and running the code examples shown, with the appropriate equals() implementation.  Then you would see whether each option satisfies the requirements that Campbell has posted.  We've given examples that highlight some strange behavior of options A and C.

Campbell Ritchie
Marshal
Posts: 64179
215

Mike Simmons wrote:. . . I didn't notice that she'd fixed the original post at that point.

Nor did I notice that editing. Sorry about that.

Campbell Ritchie
Marshal
Posts: 64179
215

emma roberts wrote:. . . i don't undrestand why these answers are correct and why a+c are not

Have our explanations helped you at all?

emma roberts
Ranch Hand
Posts: 61
thank you very much

 The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad: Create Edit Print & Convert PDF Using Free API with Java https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire