• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generic wildcards -K&B pg 593

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt in nbounder,super & extends wildcards

with <?> -> can pass anything & only NULLs can be added.
with <? extends Animal> -> can pass subtypes & NOTHING CAN BE ADDED (not even subtypes ,NULLs)
with <? sper Animal> -> can pass super types ,can add Super types & NULL



please help me clearing if its right or i am wrong somewhere
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

In each case, you can add null (if you have a list for example), as null has no type ...

With void foo(List<?> list){}
You can add no object. You can just get an Object : Object o = list.get(0)

With void foo(List<? extends Animal> list){}
You can add no object even an Animal because you can pass List<Dog>.
You can just get an Animal : Animal a = list.get(0)

With void foo(List<? super Animal> list){}
You can get an Object from the list but not an Animal because you can pass a List<Object>.
You can safely add an Animal and only an animal : if you pass List<Object> or List<Animal> it's Ok as an Animal IS-AN Object

I hope I've made no mistake ...
 
I'm doing laundry! Look how clean this tiny ad is:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic