aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why does an iterator need a cast for a generically defined list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why does an iterator need a cast for a generically defined list" Watch "Why does an iterator need a cast for a generically defined list" New topic
Author

Why does an iterator need a cast for a generically defined list

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



The above code example results in the following compiler error:



Why does the Iterator.next() method need a cast when the List associated with the Iterator is declared generically?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

O. Ziggy wrote:

The above code example results in the following compiler error:



Why does the Iterator.next() method need a cast when the List associated with the Iterator is declared generically?


It needs a cast, because you are not using generics for the iterator. Try...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thanks Henry. I thought that since the List provides the Iterator it will returned a typed iterator.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

O. Ziggy wrote:Thanks Henry. I thought that since the List provides the Iterator it will returned a typed iterator.



The iterator() method returns an object. It is you who declared the iterator reference and assigned it. There is no way to create a class, with a method, that returns an object, that can control how it will be assigned. If you want to create an iterator reference, that doesn't use generics, the compiler can't stop you. It just does what you want.

Henry
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Because of 'Type Erasure',JVM does not know at runtime that 'list' refers to a LinkedList of "Integer objects".
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok here is one that has confused me again:



This works fine and produces the output 5234252342. How come i didnt have to cast the i.next() call?

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

O. Ziggy wrote:Ok here is one that has confused me again:



This works fine and produces the output 5234252342. How come i didnt have to cast the i.next() call?



Ok i think in the compiler is not asking for a cast in the above example because the value is not being assigned to another reference. It just calls the toString() which will call the overriden toString() method of what ever instance the returned object is. right?
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

Yes you are right.'Integer val=i.next();' doesn't compile without the cast, but you may have 'Object val=i.next();'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does an iterator need a cast for a generically defined list