Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > About generics and Iterator

Reply
Thread Tools

About generics and Iterator

 
 
Neroku
Guest
Posts: n/a
 
      02-10-2007
Hello, I have a doubt about generics usage with the iterator
interface.
The following declarations should be 'equivalent', I mean, all the
methods return the same type (a reference to Object):

Iterator it;
Iterator<Object> it;

Well, now consider the following code:

Vector<Animal> v;
....
Iterator<Object> it = v.iterator();

It doesn't work, since v.iterator() returns a Iterator<Animal>
reference, which is incompatible with Iterator<Object>,
but:

Vector<Animal> v;
....
Iterator it = v.iterator();

It works fine, but v.iterator() returns an Iterator<Animal> reference,
why are both references compatible??

TIA

 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      02-10-2007
On Feb 11, 3:14 am, "Neroku" <n37...@gmail.com> wrote:
> Hello, I have a doubt about generics usage with the iterator
> interface.
> The following declarations should be 'equivalent', I mean, all the
> methods return the same type (a reference to Object):
>
> Iterator it;
> Iterator<Object> it;
>
> Well, now consider the following code:
>
> Vector<Animal> v;
> ...
> Iterator<Object> it = v.iterator();
>
> It doesn't work, since v.iterator() returns a Iterator<Animal>
> reference, which is incompatible with Iterator<Object>,
> but:
>
> Vector<Animal> v;
> ...
> Iterator it = v.iterator();
>
> It works fine, but v.iterator() returns an Iterator<Animal> reference,
> why are both references compatible??
>
> TIA


Try Iterator<? extends Object> it

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-11-2007
Neroku wrote:
> Hello, I have a doubt about generics usage with the iterator
> interface.
> The following declarations should be 'equivalent', I mean, all the
> methods return the same type (a reference to Object):
>
> Iterator it;
> Iterator<Object> it;


They are not equivalent. The first is a raw type and the second is a generic type.

> Vector<Animal> v;
> ...
> Iterator<Object> it = v.iterator();
>
> It doesn't work, since v.iterator() returns a Iterator<Animal>
> reference, which is incompatible with Iterator<Object>,
> but:
>
> Vector<Animal> v;
> ...
> Iterator it = v.iterator();
>
> It works fine, but v.iterator() returns an Iterator<Animal> reference,
> why are both references compatible??


They aren't, really. You would get an "unchecked" warning, but it is
reluctantly permitted because Iterator is the erasure of all parametrized
Iterator types. You should avoid using raw types and generic types in the same
expressions, except for the few places where Java pretty much forces you to.

Read the Java Language Specification on these topics.

-Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
EMPTY_SET.iterator() and generics Eric Sosman Java 12 03-01-2007 06:18 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
difference between the each iterator and the collect iterator? vasten@gmail.com Ruby 4 10-28-2005 03:34 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57