This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of React Cookbook: Recipes for Mastering the React Framework and have David Griffiths & Dawn Griffiths on-line!
See this thread for details.
Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Generics casting

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By doing some experimentation I have deduced that you can only cast generics using wilcards (without warning)

For example:

Casting with:
(List<?>) OK
(List<? extends integer>) OK
(List<Integer>) Warning

But I can't found any info about this. Is the rule true?
And someone knows why?


(Smilies disabled)
[ October 25, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure (List<? extends Integer> doesn't throw a warning?

anyway, the explanation is that, the cast is probably needed at runtime
but at runtime, all generic info is erased, so it makes no sense to cast
List<Integer>
whereas List<?> is the same as List, so it doesn't complain about that.
this is what I think.
 
Joan Pujol
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akshay Kiran:
are you sure (List<? extends Integer> doesn't throw a warning?

anyway, the explanation is that, the cast is probably needed at runtime
but at runtime, all generic info is erased, so it makes no sense to cast
List<Integer>
whereas List<?> is the same as List, so it doesn't complain about that.
this is what I think.



It's very strange, but I'm sure. For example

Object o=(List<? extends Integer> new ArrayList(); //No warning
Object o=(List<Integer> new ArrayList(); //Warning
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic