• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Doubt in Generics.....

Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static <E extends Number> List<? super E> check(List<E> list)
return null;

public static void main(String... a)
//Line 1
//Line 2

output = check(input);

List<Integer> input = null;
List<Integer> output = null; // Case 1


List<Number> input = null;
List<Number> output = null; // Case 2

Why inserting the above for Line 1 & Line 2 does not work......?
Here my doubt is that in case of return typ for the above function

List<? super E> if I take the input as above say case 1 then E is Integer

so the return type becomes List<? super Integer>
so here if I return List<Integer> output; then it should also work..

I am really confused with List<? super Integer>
If I am correct it means List of Integer/List of anything that is super class of Integer...
Please help............
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The wildcard return type doesn't tally with the specific type (Integer/Number). It may help to visualize the code without generics in each case aka erasure.

The code compiles if output is declared with the same range :
List<? super Integer> output = null;

The same applies when using an upper bound wildcard instantiation : <? extends E>

It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic