• 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
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Generics question

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please explain, why this snippet (K&B scjp 5 book, ch. 7 self test, q. 16, C) wouldn't compile?


The K&B explanation says "C is wrong because the return type evaluates to List<Integer>, and that can't be assigned to a variable of type List<Number>." I thought that E evaluates to Integer here, thus having <? super E> for the return type would be ok?
 
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
The return type doesn't evaluate to Integer. If you try this



In fact, the return type is not known, it says List<? super E> or in the current call List<? super Integer>, thus the list can be of any type, not necessarily Integer or Number. You can't even catch the returned value in List<Object> as the type of the returned list is actually not known. You can only catch it in an untyped list.

[Edit]

I just looked up in my book and the statement actually looks like this



Thus the explanation is correct...
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The method process returns List<? super Integer> and this cannot be assigned to List<Number> because we want output to contain only Numbers. Whereas the returned list can contain objects of any supertype of Integer, eg. Object. Since Object is-not-a Number, it could create problems later at runtime.
 
Andre Enimot
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot! I think I got that, the return type of a method can potentialy be few levels "higher" than the variable's type we are assigning it to. So if we use E = Integer, then super E can be not necessarily a Number (which would work), but even Object (which wouldn't work).
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic