Which is the best one from the following i.e List l =new ArrayList() and ArrayList al=new ArrayList()

The First one List l = new ArrayList() 
allows you to use the List1 to refer to any object that is a sub class of list 


The second one ArrayList al = new ArrayList() 
will allow you to refer to only object that is an ArrayList Object

Logically the second way should be faster. But, since the binding happens 
at runtime, it shouldnt matter really. Its actually more to do with 
polymorphism. When you know you are going to be referencing object that may 
be sub classes and want to reuse the object ref.

It's simple, List is a Interface and "Arraylist, LinkedList, Vector" are your implementation. Each one with its own benefit, when you use the List any moment you can change your implementation, your application becomes more flexible. 

Example: 
List list = new ArrayLista(); 
for(String s : list){ 
... 


list = new LinkedList(); 
for(String s : list){ 
... 

No comments:

Post a Comment