Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

generics - get and put principle

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


Book - java generics and collection by Maurice Naftalin and Philip Wadler
page no. 19


The Get and Put Principle: use an extends wildcard when you only get values out of a
structure, use a super wildcard when you only put values into a structure, and don’t use
a wildcard when you both get and put.


though book has explained it with some codes, i didn't understand what does it actually means. why we can't put the value while using wildcard (? extends T) and why cant we put the value while using (? super T) ??
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Puspender, the concept is pretty simple:

This will fail compilation at the add operation. This is because the compiler doesn't know if numberList is actually List<Integer> or List<Float> or something else (as these all fit ? extends Number). So the compiler can't let you add Integer to it. So put/add is not allowed when using extends wildcard.

Similarly, if you are using super wildcard, then put/add works but get doesn't work

Here the opposite case happens, the compiler doesn't know if numberList is actually List<Integer> or List<Number> or List<Object> (as these all fit <? super Integer>). So you can't get an element into Integer object, as the list might actually be List<Number> and might contain Float objects. But add/put works fine, because regardless of whether numberList is List<Integer> or List<Number>, Integer objects can be added to the list. Hope that helps...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it helped
thank you ankit
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic