About generics and Iterator

Discussion in 'Java' started by Neroku, Feb 10, 2007.

  1. Neroku

    Neroku Guest

    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
    Neroku, Feb 10, 2007
    #1
    1. Advertising

  2. Neroku

    hiwa Guest

    On Feb 11, 3:14 am, "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;
    >
    > 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
    hiwa, Feb 10, 2007
    #2
    1. Advertising

  3. Neroku

    Lew Guest

    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
    Lew, Feb 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    5,990
    John C. Bollinger
    May 20, 2005
  2. Hendrik Maryns
    Replies:
    18
    Views:
    1,421
  3. Eric Sosman

    EMPTY_SET.iterator() and generics

    Eric Sosman, Feb 22, 2007, in forum: Java
    Replies:
    12
    Views:
    771
  4. Soul
    Replies:
    0
    Views:
    518
  5. Replies:
    4
    Views:
    183
    Christophe Grandsire
    Oct 28, 2005
Loading...

Share This Page