Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP: Difference between ArrayList al = new ArrayList() and List al = new ArrayList()? [duplicate]

Possible Duplicate:
List versus ArrayList

Difference between

ArrayList al = new ArrayList() 

and

List al = new ArrayList() ?
like image 299
Hari kanna Avatar asked Jan 04 '11 10:01

Hari kanna


People also ask

What is difference between List l New ArrayList () and ArrayList a new ArrayList ()?

When you declare List l = new ArrayList() l is limited to the methods provided by the interface List, while when you declare it ArrayList al = new ArrayList() it can use all methods and fields in an arraylist.

What is the difference between ADD () and Set () when using ArrayList?

ArrayList set() Vs. However, there is a major difference between them. The set() method adds a new element at the specified position by replacing the older element at that position. The add() method adds a new element at the specified position by shifting the older element towards the right position.

What is the difference between ArrayList and ArrayList <?> In Java?

The List is an interface, and the ArrayList is a class of Java Collection framework. The List creates a static array, and the ArrayList creates a dynamic array for storing the objects. So the List can not be expanded once it is created but using the ArrayList, we can expand the array when needed.


3 Answers

None, from a creation perspective. Both create an instance of ArrayList.

The difference is that, in you second example, al allows access to all methods implemented on the List interface while, in the first example, al allows access to all (accessible) methods and fields of the ArrayList class.

A practical rule of thumb: use the second pattern. If you need some extra goodies from the ArrayList implementation, then you can always cast:

 List list = new ArrayList();
 // do some adds/removes/... on the list
 ((ArrayList) list).trimToSize();
like image 92
Andreas Dolk Avatar answered Oct 25 '22 18:10

Andreas Dolk


Its called programming to interface. Suppose, you need to return this list from your method. So the calling code can have that into a List variable.

public ArrayList getList() {
   ArrayList list = new ArrayList();
    // do something with the list here
   return list;
}

And this,

public List getList() {
   List list = new ArrayList();
    // do something with the list here
   return list;
}

Now for the latter method calling code can have the returned list in List type variable. And you can easily decide later, for some reason, something like this,

public List getList() {
   List list = new LinkedList();
    // do something with the list here
   return list;
}

No change in calling code, whereas with the former you need to change the return type, that would eventually screw up the calling code as well.

like image 43
Adeel Ansari Avatar answered Oct 25 '22 19:10

Adeel Ansari


Paraphrasing my answer to this very similar question about Map vs. HashMap:

There is no difference between the objects. There is a difference in the interface you have to the object. In the first case, the interface is ArrayList, whereas in the second it's List. The underlying object, though, is the same.

The advantage to using List is that you can change the underlying object to be a different kind of list without breaking your contract with any code that's using it. If you declare it as ArrayList, you have to change your contract if you want to change the underlying implementation.

like image 45
T.J. Crowder Avatar answered Oct 25 '22 17:10

T.J. Crowder