• 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

Generics <?> is the same as <? extends Object>

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

I was reading through the K&B SCJP 6 book and maybe I read it wrong but it said that <?> was equivalent to <? extends Object>. I was wondering if that is true. I am asking because of a little inconsistency I saw when trying out some code. When running the below code it compiled just fine.



But compiling it with <? extends Object> I got a compiler warning.



Can somebody clarify this for me. Thanks in advance.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are not equivalent. <?> has no type parameter specified. Any object you try to add will have a type and therefor be disallowed. The exception is null because it has no type. However that is quite useless, isn't it? <? extends Object> sets Object as the upper bound and will accept only additions of type Object. Upcasting is not implicit with generics because that would ruin the whole point.

To make you code work you should pass a type to the instantiation. Like this.



This article is excellent, and fully explains the use of generics.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<? extends Object> sets Object as the upper bound and will accept only additions of type Object.


Are you sure this is the case? If you do

then you get a compilation error. This makes sense as the list passed as parameter could be of any type and for a List of type <String> you cannot add an instance of Object.
Do I misunderstand anything?

John
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think you understand it correctly. To repeat what you said in different words: "List<? extends Object>" doesn't mean a List which can include anything which extends Object. It means a List of some unknown type which extends Object. For example a "List<File>" could be such a List.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic