wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes CelPal - SCJP 1.5 Part 3 - Question 26 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "CelPal - SCJP 1.5 Part 3 - Question 26" Watch "CelPal - SCJP 1.5 Part 3 - Question 26" New topic
Author

CelPal - SCJP 1.5 Part 3 - Question 26

Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Given this fragment

Toll toll = new Toll();
Toll toll2 = new Toll();

What must be done so that the JVM understands whether toll and toll2 are the same or not ?

1.) Override public boolean hashCode() in Toll
2.) Override public int hashCode() in Toll
3.) Override public boolean toString() in Toll
4.) Override public boolean equals() in Toll
5.) Override public boolean equals(Object o) in Toll

The corret answers are 2) and 5), and the explication for the author is:

"The equals and hashCode methods must be overridden to compare any 2 objects"

But for me the unique answer correct is the number 5).

What do you think?

Thanks!

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30369
    
150

I agree with 2 and 5. It is bad practice to implement equals() without implementing hashCode(). It breaks the contract of Java.

Also, the question doesn't specify how you are checking for equality. What if the objects are in a HashMap? Java will look at the hashcode first, see they are different and never get to calling equals


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Hi Jeame, and thanks for your response!

I understand when you say "It is bad practice to implement equals() without implementing hashCode()" and I am agree with you. It's ok.

But, the question was: "What must be done so that the JVM understands whether toll and toll2 are the same or not?" and for me it enough only the answer 5).

About the options, for me:

5) We MUST do it
2) We SHOULD do it. It is correct, but it is not necesary.

What do you think? Am I wrong?

Here, an example:


A lot of thanks!
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
It is not clear what is meant by "JVM understands" but assuming that the question is trying to compare the contents of the objects, option 5 is correct. Of course, as Jeanne said, it is a good practice to override hashCode if you are overriding equals, but from a strictly comparison perspective, overriding hashCode is not required.

HTH,
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Paul, Thanks for help us!
 
 
subject: CelPal - SCJP 1.5 Part 3 - Question 26