• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Generics and Collection

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was doing some mocks and I faced a question about generics and collection.

That question make me think and I did a simple main program :



My question is:
why this compiles and works fine ?

The output is

To what I've understood about generics and collection, if I declare a collection with the wildcard "?", I should not be able to do an "unsafe" operation, like add, to the collection.
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I got it...
if you use the keywork "? extends something" you can't add, while if you use "? super something" you can add anithing that is of class "something" or a superclass of something.

Am I right ?!
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Emanuele,

You are absolutely correct, when we say something like


Then we can safely do the add operations. Because the above statement says, only sub-type of Number
is allowed to be added, anything lower in the hierarchy is not allowed.

Today, i was away from ranch, these days even i am dreaming of java-ranch.

Best Wishes,
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I confused here? I thought if you declare:


you can add a subtype of Number, not a supertype of Number to the collection. e.g. we can add an Integer.

Am I mistaken?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant to say, it can accept any super type as an argument. But addition can be Number or any
subtype of Number only.

Hope this helps,
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Phillip Wells wrote:you can add a subtype of Number, not a supertype of Number to the collection. e.g. we can add an Integer.


Phillip you are right. When we say <? super Number> we can add Number or its sub-types to that collection...
 
Phillip Wells
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep, that's what I thought. It will accept a List that has elements of Number (or a superclass of Number) and within the method you can add a Number or any sub-type of Number. Ankit - Snap lol
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure the output is only 7?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Florin Florentin wrote:are you sure the output is only 7?



No the output should be

[9,7]

Hope this helps,
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:

Florin Florentin wrote:are you sure the output is only 7?



No the output should be

[9,7]

Hope this helps,



Yes, the output is [9,7] ;)
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic