About Question enthuware.ocpjp.v11.2.3407 :

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

Moderator: admin

Post Reply
gadsgadsx
Posts: 7
Joined: Fri Apr 12, 2024 11:09 pm
Contact:

About Question enthuware.ocpjp.v11.2.3407 :

Post by gadsgadsx »

In the explanation part, what is the meaning of this part:

"While the collection cannot be modified via the unmodifiable view, the underlying collection may still be modified via a direct reference to it. However, the collections returned by the of/ofEntries API methods are in fact unmodifiable."

The text states that methods unmodifiableXXX() creates unmodifiable views, but the underlying collection may still be modified, whereas this is not the case with of/ofEntries. But I tried the code below, using of(), and changed the original arraylist. It did modified the reference from List.of(). So, what is the meaning of "API methods are in fact unmodifiable"?

public static void main(String[] args) {
Collection<Number> col = new HashSet<>();
col.add(1);
var list1 = List.of(col);
col.add(2);
System.out.println(list1); //[[1, 2]]
System.out.println(col == list1.get(0)); //returns true, showing it points to the same object
}

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

Re: About Question enthuware.ocpjp.v11.2.3407 :

Post by admin »

In your example, list1 points to an immutable List that contains a HashSet pointed to by col. You are adding 2 to col (ie the HashSet) not to the immutable list.
If you like our products and services, please help us by posting your review here.

gadsgadsx
Posts: 7
Joined: Fri Apr 12, 2024 11:09 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3407 :

Post by gadsgadsx »

So, immutable only means that the I cant add/remove/alter elements point by the immutable reference, but changes in the underlying object (the hashset, in this case) are still reflected in my immutable reference? Except for the copyOf() method?

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

Re: About Question enthuware.ocpjp.v11.2.3407 :

Post by admin »

No, first of all, references are not mutable or immutable. Only the objects pointed to by the references can be mutable or immutable. Second, an immutable object doesn't necessarily mean that its members are also immutable. An object may have an instance field of a mutable type.

So, the List object pointed to by list1 is immutable. You can't add or remove elements from this list. You can't do that using any reference because this list object is immutable. However, one of the elements of this list happens to be a mutable Collection. You can add or remove elements to/from that collection.
If you like our products and services, please help us by posting your review here.

abcackle
Posts: 1
Joined: Thu Apr 25, 2024 3:55 am
Contact:

Re: About Question enthuware.ocpjp.v11.2.3407 :

Post by abcackle »

admin wrote:
Thu Apr 25, 2024 2:35 am
No, first of all, references are not mutable or immutable. Only the objects pointed to by the references can be mutable or immutable. Second, an immutable object doesn't necessarily mean that its members are also immutable. An object may have an instance field of a mutable type.

So, the List object pointed to by list1 is immutable. You can't add or remove elements from this list. You can't do that using any reference because this list object is immutable. However, one of the elements of this list happens to be a mutable Collection. You can add or remove elements to/from that collection.
That's a great suggestion! References can only point to changeable or immutable things.
I'm a Recreational Therapist!space bar clicker

gadsgadsx
Posts: 7
Joined: Fri Apr 12, 2024 11:09 pm
Contact:

Re: About Question enthuware.ocpjp.v11.2.3407 :

Post by gadsgadsx »

Thanks for the clarification!

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests