Page 1 of 1

About Question enthuware.ocpjp.v11.2.3407 :

Posted: Tue Apr 23, 2024 7:38 pm
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
}

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

Posted: Tue Apr 23, 2024 10:54 pm
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.

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

Posted: Wed Apr 24, 2024 5:50 pm
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?

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

Posted: Thu Apr 25, 2024 2:35 am
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.

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

Posted: Thu Apr 25, 2024 4:00 am
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.

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

Posted: Thu Apr 25, 2024 6:36 am
by gadsgadsx
Thanks for the clarification!

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

Posted: Fri May 17, 2024 1:40 am
by bettyking
gadsgadsx wrote:
Tue Apr 23, 2024 7:38 pm
In the explanation part, what is the meaning of this part: io games

"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
}
This statement describes the behavior of unmodifiable views in Java Collections.