This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

equals method  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the following snippet of code of the certification bokk SCJP

Select the three correct answers.
(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;
the correct answer is b+d+e
i don't undrestand why these answers are correct and why a+c are not
 
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Foreman
Posts: 3254
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry but i also stay don't undrestand
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 Foreman
    Posts: 3254
    19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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 Foreman
    Posts: 3254
    19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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: 64496
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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: 64496
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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: 62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you very much
     
    CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
    how do I do my own kindle-like thing - without amazon
    https://coderanch.com/t/711421/engineering/kindle-amazon
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!