• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

A question from the Master Exam CD (generics)

 
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is regarding generics.

In the below program, the return type (alist2 and alist3) meet the conditions set in the return portion of the method signature (which is List<? extends T>). Yet, I get an error. Thanks in advance. The program follows:

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please post the error?
 
Greenhorn
Posts: 17
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this
 
Nikhil Pujari
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, here it is:
 
Nikhil Pujari
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ravi: Explicit casting works, but I wonder why I need it. It seems redundant.

Edit: I think I get it. Please tell me if I am wrong. if T is "Number", List<? extends T> can return List<Short> or List<Float>. These cannot be caught in a List<Integer>, hence the cast.

You need a cast for List<Number> because, the only thing a List<Number> variable can point to, is a List<Number>. It cannot point to a List<Integer>, even though Integer is-a Number.

Is this right?
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Pujari wrote:@ravi: Explicit casting works, but I wonder why I need it. It seems redundant.

Edit: I think I get it. Please tell me if I am wrong. if T is "Number", List<? extends T> can return List<Short> or List<Float>. These cannot be caught in a List<Integer>, hence the cast.

You need a cast for List<Number> because, the only thing a List<Number> variable can point to, is a List<Number>. It cannot point to a List<Integer>, even though Integer is-a Number.

Is this right?


pretty much.
A List<Number> can be assigned to a List<? extends Number> but a List<? extends Number> cannot be assigned to a List<Number> (which is what you are trying to do in line 18)
A List<Integer> can be assigned to a List<? extends Number> but a List<? extends Number> cannot be assigned to a List<Integer> (which you are trying to do in line 17)
 
Nikhil Pujari
Greenhorn
Posts: 21
Firefox Browser C++ Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Pujari wrote:@ravi: Explicit casting works, but I wonder why I need it. It seems redundant.




In my opinion, if you are using generics, you should not be explicitly casting. It is not redundant. If the compiler is complaining, it is because they compoler considers it not safe. If you override the compiler by explicitly casting, at best, you are not letting the compiler do its job. And at worst, you are wrong (and the compiler is right) and you may have broken something in your program.


As for your example....

A List<? extends Number> is *NOT* assignable to a List<Integer>, hence, the compiler error.

A List<? extends Number> is *NOT* assignable to a List<Number>, hence, the compiler error.




Now again, for your example, your method is returning a List<Number>, so with your explicit casting...

The first line of the example is actually broken, it is trying to assign a List<Number> to a List<Integer>, which is *NOT* type safe. From within the method, it is possible to put Number types that is not Integer into the list, which you then explicit cast to List<Integer>. So, in the first cast, you were wrong, and the compiler right, and you could have broken something.


Henry
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic