wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic type for Iterator is also required ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic type for Iterator is also required ?" Watch "Generic type for Iterator is also required ?" New topic
Author

Generic type for Iterator is also required ?

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi, This is from Whizlabs and question is given in the comments


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Sep 23, 2010
Posts: 422
Thanks Abi and Oet
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3361
    
    9
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.
 
Consider Paul's rocket mass heater.
 
subject: Generic type for Iterator is also required ?
 
Similar Threads
generics code mixed with legacy collection
New type of for loop?
Arraylist
Multiple thread accessing List (ArrayList, Vector)
Why does an iterator need a cast for a generically defined list