Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Doubt about ? super

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,guys.

I have a question about ? super



A is a superclass of B ,in line2 why I cann't add A to l2?

Best Regards!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following:



You will get a runtime ArrayStoreException on line 2. This is pretty similar to what you're trying to do with generics only with generics the compiler will catch the exception in compile time and will not let you do it.

If you change line 2 to:


You will no longer get a runtime error. Same thing for if you try to add A to your ArrayList. The reason, i assume and I hope people will correct me if i'm wrong, is that the underlying type of the ArrayList is a A and that's the only type you can add to the list because "?" is a runtime placeholder so basically Java can't know what you'll put in there but it will know that any thing that comes out of the list when you "get" it will be an A.

HTH
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Noam Wolf:
You will get a runtime ArrayStoreException on line 2.


Actually, I believe you'll get a compile time error on the previous line.
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can interpret ArrayList<? super B> to mean that the list contains elements only of some specific unknown type, but we do know that the type is either B or a superclass of B. An ArrayList<A> would fit, but so would an ArrayList<B>. Therefore you could have written this for line 1 instead:

In this case, it's clearer why line 2 fails -- it's trying to put something that's not a B into a list of B's. It fails to compile in your original example because the compiler doesn't really know exactly what the specific type of l2 is, so it behaves pessimistically.
 
Noam Wolf
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Russel - the ArrayStoreException happens for the non-generic example... try it out.
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Noam Wolf:
@Russel - the ArrayStoreException happens for the non-generic example... try it out.



I did. It doesn't compile.
 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Russell, Thank you for your explanation.


Best Regard.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic