0

I've got following method:

public Iterator<Item> iterator() {
    return new ListIterator();
}

and it's used in following code:

Deque<Integer> deque = new Deque<Integer>();
// ...
Iterator<Integer> iter1 = deque.iterator();
Iterator iter2 = deque.iterator();

The question is - why does java compiler accept both lines (iter1 & iter2)? Especially the iter2 is strange for me. When I create my former object (deque) I need to specify the type on both sides.

Anyway, if I put

Deque deque2 = new Deque();

then compiler is fine with this too. Confusing...

ducin
  • 25,621
  • 41
  • 157
  • 256

1 Answers1

3

why does java compiler accept both lines

That is there for backward compatibility. Generics were included only in Java 1.5. So, if it wasn't allowed to use raw types (that's what they are called), then all the previous code would have failed to compile. However, even though you're allowed, you shouldn't use them in newer code. Wherever you use raw type, the compiler will give you an Unchecked Warning.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Is the old structure (raw types) going to last forever for backwards compatibility? Or is it going to be cut off (like Python 3 did to some functionalities from Python 2)? – ducin Feb 16 '14 at 09:17
  • @tkoomzaaskz I don't think it is going to be removed in near future. At least for now, there is no plans as such. – Rohit Jain Feb 16 '14 at 09:22