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

Generic type for Iterator is also required ?

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, This is from Whizlabs and question is given in the comments
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The iterator method returns generically, but, you assigned it to a non-generic collection. So, you break the generics there, so, you need a cast!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:The iterator method returns generically, but, you assigned it to a non-generic collection. So, you break the generics there, so, you need a cast!


The typed collection that the topic starter is using returns an typed Iterator. However he is then assigning it to a non-typed Iterator so the type information is lost. And because of that the cast is needed.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:
The typed collection that the topic starter is using returns an typed Iterator. However he is then assigning it to a non-typed Iterator so the type information is lost. And because of that the cast is needed.


This is what I intended to mean, but, may be interpreted slightly different. Does it?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abi and Oet
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it gets erased during compilation. There's no way for the JVM to know what kind of Iterator i is on line 9. It may have changed in the mean time. The compiler recognizes this and forces you to cast the object reference.

This isn't necessary if the Iterator has a generic type, because even though the JVM still won't know what kind of iterator i is at line 9, the compiler sees that it can only return references of that particular type, so you don't have to cast.

Get into the habit of never using raw types. Always parameterize generic types.

[edit]

The question in the last post has been edited away.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic