About Question enthuware.ocpjp.v8.2.1380 :

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

Moderator: admin

Post Reply
mrmuiz
Posts: 49
Joined: Mon Jul 27, 2015 4:34 am
Contact:

About Question enthuware.ocpjp.v8.2.1380 :

Post by mrmuiz »

You can read here that
A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.
but I can successfully compile and run this

Code: Select all

Map<Map,String>map=new HashMap<>();
map.put(map,"");
Does this mean that it is sintactically allowed (it will compile) but semantically it won't work because of equals and hashCode methods are not well defined?

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

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

Post by admin »

Yes, that is correct. Compiler cannot know what exact object map variable points to at run time and it cannot execute the code of put method to determine that putting map as the key is not permitted, and that is why it permits the call at compile time.
If you like our products and services, please help us by posting your review here.

julianblk
Posts: 1
Joined: Thu Jan 11, 2018 9:26 am
Contact:

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

Post by julianblk »

Then shouldn't the first option read:

It is not permissible for a map to contain itself as a key.

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

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

Post by admin »

In that case, it would be a correct option. As of now, it is an incorrect option. So I think it is ok.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ildar_S
Posts: 5
Joined: Fri May 03, 2019 9:55 am
Contact:

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

Post by Ildar_S »

Code: Select all

Map<Map, Integer> m = new HashMap<>();
m.put(m, 3);
System.out.println(m.size());
Compiles and runs successfully, shouldn't the first option be correct then?

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

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

Post by admin »

From JavaDoc API description on Map:
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a map.
https://docs.oracle.com/javase/8/docs/a ... l/Map.html
If you like our products and services, please help us by posting your review here.

Ildar_S
Posts: 5
Joined: Fri May 03, 2019 9:55 am
Contact:

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

Post by Ildar_S »

Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests