Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return type of iterator() method in java

I am a new-comer in Java and in process of learning. I need a an answer of following question supported with valid theory. Consider the following line-

Iterator itr = al.iterator(); 

where al is some collection object of ArrayList (class) type. I want to know what is here the return type of

al.iterator()

It is not definitely of a primitive data type, then it could be an object , but since every object belong to a class then it is of which class. Documentation and books etc says it has return type of Iterator. But Iterator is an interface. On other hand we say an interface could not have a direct object. Although interface variable could refer to an object of a class or classes that implements it.

-

So the above syntax is correct (as Iterator variable itr could be used to refer to an object of some class implementing it). But in realty it is object of which class? And can substituting itr with reference variable of that class will cause no error (I have tried substituting itr with ref. variable of ArrayList class in above line but that causes an error). I am using this syntax very often also in Generics form, but dont know the theory behind this. And I am supposing I am lacking a very basic concept here. Please rectify.

like image 719
user3528086 Avatar asked Feb 14 '23 05:02

user3528086


2 Answers

I want to know what is here the return type of al.iterator()

You shouldn't care. It doesn't matter what particular implementation of Iterator the method returns, only that it returns an Iterator. You can call next, hasNext, and remove, or iterate with a for-each loop, without knowing that it happens to be an ArrayList.Itr or whatever the implementation is.

Suppose the Java developers working on the ArrayList want to rename their iterator implementation to ArrayListIterator instead of whatever they're calling it now. If your code relied on knowing the exact Iterator implementation, you would have to change your code just to handle an ArrayList-internal change you shouldn't even see.

like image 178
user2357112 supports Monica Avatar answered Feb 27 '23 21:02

user2357112 supports Monica


You are right saying that Iterator is an interface. So, some class must implement this interface so you can use it.

The iterator interface defines what an iterator should do. In this case, the class ArrayList provides its own implementation of this interface.

What you get from al.iterator() is the inner class of ArrayList Itr (see following code, which IS iterator. (as we kind of describe the IS-A relationship)

(you can think of an inner class as a normal class for now. So the answer to your question is Itr, which is just a name, and all you should care about is its function, which is defined by interface Iterator.)

   /**
     * An optimized version of AbstractList.Itr
     */
    private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor != size;
        }
       .....
  }

This code is by Josh Bloch and Neal Gafter, who wrote the ArrayList class.

like image 38
Weishi Z Avatar answered Feb 27 '23 23:02

Weishi Z