List
is an interface, essentially providing a list of operations (add, remove, get...), but no implementation (you cannot do new List
). There are several classes implementing List
interface, including ArrayList
(using, as said, an array as internal container) and, for instance, LinkedList
. You can instantiate these, instead, and write:
List<ElementType> myList = new ArrayList<ElementType>();
Using List
as a type for myList
reduces the effort if you want to replace ArrayList
with LinkedList
:
List<ElementType> myList = new LinkedList<ElementType>();
(immagine, instead, if you had to replace ArrayList
with LinkedList
in several places, instead). Additionally, you will hide the actual implementation lying behind, so that other programmers don't make tricky assumptions on how the List
might behave.