Difference Between List and ArrayList ?

List is an interface and ArrayList is an implementation of the List interface. The arraylist class has only a few methods in addition to the methods available in the List interface. There is not much difference in this. The only difference is, you are creating a reference of the parent interface in the first one and a reference of the class which implements the List (i.e) the ArrayList class in the second. If u use the first, you will be able to call the methods available in the List interface and you cannot make calls to the new methods available in the ArrayList class. Where as, u are free to use all the methods available in the ArrayList, if u use the second one.

To add some more points to this,

1. I would say the first approach is a better one because, when you are developing java applications, when you are supposed to pass the collection framework objects as arguments to the methods, then it is better to go with


List tempList = new ArrayList();
somemethodcall(tempList);

Why am i saying this because, in future due to performance constraints, if you are changing the implementation to use linkedlist or someother classes which implements List interface, instead of ArrayList, you can change at only one point (i.e) only the instantiation part. Else u ll be supposed to change at all the areas,whereever, u have used the specific class implementation as method arguments.

You cannot check for performance in this. Instead u can check for performances of the implementing classes of List interface in the following link. It explains you about the performance statistics of the collection framework

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. Thanks , that was helpful.

  2. sandip sabat says:

    List lp=new ArraList(); and ArrayList ls=new ArrayList( )

    Please tell me the memory allocation in Both of them means which one loads at compile time and which one on run time allocating of memory for the elements they contain?

Speak Your Mind

*