• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does wildcard, extends, ensures Immutability  RSS feed

 
Aladdin Raul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm studying the book Java Generics and Collections, Still in 1st part, Generics, Now while studying faced few confusing or contradicting, in chapter 2, subtyping and wildcards, section 2.4 the get and put principle, Author says that I can add elements to a list packed up by Arrays.asList() method, as far as I know list packed by asList() method is fixed, I can't add or remove elements from it, here is the snippet from the book :




Author says also that extends wildcard does not always mean immutability, that I can remove elements or all elements of particular list of <? extends T> type parameter, here is another snippet from the book :

It is tempting to think that an extends wildcard ensures immutability, but it does not. As we saw earlier, given a list of type List<? extends Number>, you may still add null values to the list. You may also remove list elements (using remove, removeAll, or retainAll) or permute the list (using swap, sort, or shuffle in the convenience class Collections


Now I'm kinda mixed up, I might misunderstood what the author wants to deliver, Would you just mind shed some light on it, Thank you .

[Rob: changed code tags into quote tags]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you're right - a List created by Arrays.asList cannot have any elements added or removed. The only way you can change it is by replacing elements (using the set method).

As for the immutability - although you cannot add any objects to a List<? extends T>, you can indeed add null, and you can also remove elements. In other words, the extends wildcard never ensures immutability.
 
Aladdin Raul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for putting time into helping me, But I gave it a whirl to remove an element ended up getting exception, do me a favor and check out my hopeless snippet of code :




 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aladdin Raul wrote:Thank you for putting time into helping me, But I gave it a whirl to remove an element ended up getting exception, do me a favor and check out my hopeless snippet of code :

That's because the List returned by Arrays.asList() is fixed (ie, it doesn't allow element addition or removal), so the message you're getting has nothing to do with your use of wildcards. Try using a standard List type, such as ArrayList instead.

[Edit]: Don't forget that generics messages occur at compile time, not at run time.

Winston
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a difference between getting code to compile and getting code to run. The compiler is not smart enough to see that the List came from Arrays.asList, and therefore does not know that you can't remove any elements. The JVM does know this during runtime, and that's why you get the exception.
 
Aladdin Raul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Aladdin Raul wrote:Thank you for putting time into helping me, But I gave it a whirl to remove an element ended up getting exception, do me a favor and check out my hopeless snippet of code :

That's because the List returned by Arrays.asList() is fixed (ie, it doesn't allow element addition or removal), so the message you're getting has nothing to do with your use of wildcards. Try using a standard List type, such as ArrayList instead.

[Edit]: Don't forget that generics messages occur at compile time, not at run time.

Winston


You are dead right, my generic list has not connection with the exception I get, thank you very much .
 
Aladdin Raul
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:There is a difference between getting code to compile and getting code to run. The compiler is not smart enough to see that the List came from Arrays.asList, and therefore does not know that you can't remove any elements. The JVM does know this during runtime, and that's why you get the exception.


You're pretty right about that, thank you .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!