• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

? extends Number - doesn't allow Integer

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

why doesn't the add line here work?



I would have thought since Integer extends Number it should allow it?

Thanks as always
 
Bartender
Posts: 15743
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would work if list was a List<Number>. Because Integer extends Number, you can add an Integer to a List<Number>.

But list isn't a List<Number>. It is a List<? extends Number>, meaning a list of some unknown type. At runtime, it could refer to a List<Byte>. I hope you agree that adding an Integer to a List<Byte> is a bad idea.
 
reply
    Bookmark Topic Watch Topic
  • New Topic