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
Latest posts by Krishna Srinivasan (see all)
- 5 New Features in HTML5 - November 29, 2013
- How To Set Tomcat JVM Heap Size in Eclipse? - November 28, 2013
- How To Resolve “Resource Is Out Of Sync With The Filesystem” Error in Eclipse? - November 28, 2013