This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generics problem (? extends Object)

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

List<? extends Object> alist = new Vector<String>();
alist.add("USA"); // compile time error

we know that this is giving compiler error because at compile time we don't know what the type is(like ? stands for what)hence at compile time if we try to add anything it will give error..

but my doubt is what is the use of new Vector<String>(); ??
Isn't new Vector<String>() is the runtime type of <? extends Object>
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new Vector<String>() is not the runtime type of <? extends Object> but it
is the runtime type of alist at the point of error. We don't know how it will change through the course of program. It's runtime type can keep on changing down the line if we make another assignment. since the runtime type cannot be determined at compile time, generics is limited only to the compile type checking. Hence, the add method on aList is checked for its type on compile type rather than runtime type. This is my guess.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At runtime generics doesnt exist. It is only for the compiler
 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lucius can you explain me in detail. I got your point that Generics exists only at compile time..So, At compile time the <String> on right side of the expression should have some use, Shouldn't it??

And if we can't add anything into <? extends Object> then what is the use of it.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic