• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

CSG17 Chapter 9 Unbounded Wildcards question

 
Greenhorn
Posts: 12
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

in CSG17 Ch9 there is a code sample:

Book says that both x1 and x2 returns Object when get() method is called.
I want to test it so I am trying to fill it first.
f.e. this code is just fine:

but when I try this:

I get an error:

So my question is: Why am I not able to fill the first list?
Thanks
 
Ondrej Ducy
Greenhorn
Posts: 12
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I have found answer in next section. It seems that unbound and upperbound wildcards creates logically immutable lists.
Sorry for spam.
 
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider this:

If you could add an Object to a List<?>, it would also allow you to add an Elephant to a List<Tiger>, which obviously isn't a good idea.
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ondrej Ducy wrote:Ok, I have found answer in next section. It seems that unbound and upperbound wildcards creates logically immutable lists.
Sorry for spam.


This is not exactly true.

The list itself is not immutable. You just can't add anything to it using that particular variable. If you have a different reference to the same list, it might be possible to modify the list through that reference.
 
Ondrej Ducy
Greenhorn
Posts: 12
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:If you could add an Object to a List<?>, it would also allow you to add an Elephant to a List<Tiger>, which obviously isn't a good idea.


It was not obvious to me at first time, but when I read that in next section of book, it made sense.
Thanks for help. ;-)
 
Ondrej Ducy
Greenhorn
Posts: 12
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: If you have a different reference to the same list, it might be possible to modify the list through that reference.


Ok, thats interesting, but how would I get reference to that list?
I tried this and it doesnt work:

It seems to me that only possible soulution would be to have a reference to the list before assigning to x1 variable.
Or is there another way?
 
Stephan van Hulst
Saloon Keeper
Posts: 15276
350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ondrej Ducy wrote:It seems to me that only possible soulution would be to have a reference to the list before assigning to x1 variable.
Or is there another way?


No, that's exactly right:

 
Ondrej Ducy
Greenhorn
Posts: 12
IntelliJ IDE Firefox Browser Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Ondrej Ducy wrote:It seems to me that only possible soulution would be to have a reference to the list before assigning to x1 variable.
Or is there another way?


No, that's exactly right:



Ok, now its clear to me. Thank you!
 
You can't have everything. Where would you put it?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic