What's the difference between declaring ArrayList
and List
for lists in Java
? What are the advantages of using one or the other?
What's the difference between declaring ArrayList
and List
for lists in Java
? What are the advantages of using one or the other?
It has already been said in the other answer the difference between them but there are some important ones about it.
The first form is preferable whenever possible since it is more generic. So if you want to change the implementation, for a LinkedList or a custom list of yours, for example, it is still possible to do maintaining code compatibility.
The rule is that you should always declare the most generic type possible. Not that this needs to be done always, but whenever a change can affect compatibility. Depending on where it is declared, if it is something very internal, it may not affect anything, but if it is part of a public API it can affect.
This holds true for any type, not just interfaces. Interface preference is even more important as it improves encapsulation and decouples design .
Because of the polymorphism even if you declare the second form you can use the created object anywhere that accepts a List
, after all a ArrayList
is a List
always.
Another advantage is that it makes it easier to write tests in this way. If the type is more general it is easier to switch the implementation to one that performs the test more appropriately. Using the interface facilitates the pattern of control inversion .
When you declare a parameter or as a member of a class, for example, using the interface, it is a way to make the method or member more general. When declaring a local variable is a way to protect yourself from using deployment members.
When you declare the variable with a type greater than that of the implementation you are telling the compiler that the object there can only do operations of this type, and any attempt to access members of the concrete type will produce an error even though the operation is possible.
Of course using the more generic type you get a little limited. You will not be able to call all methods available in the actual implementation. That is why it is said that you should use the more general type "if possible". If you need these specific methods of the more specific type, then you have to use the second form.
The memory consumption will be equivalent to that of the type whose implementation was used. That is, in the example the memory consumption will be what is required for ArrayList
. In the example neither could be the consumption of Array
because the type Array
does not consume memory since interfaces have no state, therefore they do not use memory. But since they are not implementations they can not be used to create objects.
On the other hand it is likely that the form using the interface adds a slight extra consumption of processing since there will be an indirection. It is not something important but it is good to know that there is this cost.
Consider also the use of a Collection
type that is even more general.
One last note. This non-generic form is not recommended. Prefer to create lists with defined types, something like:
List<String> myList = new ArrayList<String>();
In short, this is a form of generalization. The advantages are ease of code and test maintenance .
Some questions here that may be useful to better understand these things:
List is an interface. it defines the behavior of collections api lists.
ArrayList is a type of imlpementation of this list, as well as LinkedList is also an implementation of List.
When you set it this way:
List myList = new ArrayList();
You can only call methods and members that belong to List
. If you set it as:
ArrayList myList = new ArrayList();
You will be able to call specific ArrayList methods and specific members use ArrayList in addition to those inherited from the List.
Link to response in English: Link
Oracle text with List implementations: Link