About Question enthuware.ocpjp.v8.2.1137 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

About Question enthuware.ocpjp.v8.2.1137 :

Post by __JJ__ »

This is a tricky question. I had thought, as long as a subset of fields used in equals is used in hc, and equal objects produce equal hashCodes, then at least superficially (ie ignoring things here like whether the fields might change during the pgm) it's all good.

I wasn't expecting having to work out a set of possible field values for which instances of the class might be calculated to be equal but which would return unequal hashCodes. But I guess it's here for a reason....

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by admin »

The only thing that you need to see here is whether the given implemention of hashcode returns the same hashcode for two objects if the equals method for those two objects returns true.
You need to be able to spot this problem with the given hashcode and equals implementation for simple examples like the one given here.
If you like our products and services, please help us by posting your review here.

bortnikov_sa@mail.ru
Posts: 8
Joined: Wed Oct 25, 2017 2:53 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by bortnikov_sa@mail.ru »

Hello. Why this implementation of hashCode is incorrect?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by admin »

Please go through the explanation carefully. It explains exactly why this hashCode is not correct with example. It does not satisfy the requirement of a valid hashCode method.
If you like our products and services, please help us by posting your review here.

derlinio
Posts: 1
Joined: Sun Jun 10, 2018 12:54 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by derlinio »

But the hashcode returns the same value! for both the objects how is it incorrect?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by admin »

You really need to read the explanation carefully.
If you like our products and services, please help us by posting your review here.

nerbinwalnoot
Posts: 1
Joined: Fri Nov 22, 2019 2:15 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by nerbinwalnoot »

I had the correct answer, but i indeed did not understand it. In my question it says:

Info i1 = new Info("aaa", "aaa", "aaa"); Info i2 = new Info("aaa", "bbb", "ccc");

These will have the same s1 variable, so it is actually a correct implementation, right?

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by bvrulez »

It is indeed an invalide implementation of "hashcode" with respect to "equals", but in general according to oracle, "hashcode" needs not to be different for unequal objects.

https://stackoverflow.com/questions/164 ... e-hashcode

https://docs.oracle.com/javase/7/docs/a ... bject.html

What bothers me is that a HashMap should distinguish by hashcode, should it not? How are two objects with the same hashcode handled in a HashMap? Or does HashMap create its own hash value for storing each key?

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1137 :

Post by admin »

bvrulez wrote: What bothers me is that a HashMap should distinguish by hashcode, should it not?
Yes, but only partially.
How are two objects with the same hashcode handled in a HashMap? Or does HashMap create its own hash value for storing each key?
You will need to read about it from a book or just from the JavaDoc description of HashMap because it is too much to write in post. Let us know if you don't understand anything.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 78 guests