What is the difference between declaring List<Integer> vs ArrayList<Integer>?

collections, java

Solution

In your statement 1, since you are referring `mylist` as `List<Integer>` while it is still `ArrayList<Integer>`, hence you can use the methods available in the `List` interface ONLY. This is better statement, if you are using cross class.method functionality.

Also any method accepting `List<Integer>` can accept any of the implementation classes of `List` e.g. `LinkedList<Integer>` or your custom implementation class.

Your second statement, creates and references the object as `ArrayList<Integer>` ONLY. Some people perefer it when `mylist` is to be used locally in the method.

Problem

``` List<Integer> mylist = new ArrayList<Integer>(); ArrayList<Integer> mylist2 = new ArrayList<Integer>(); ``` I am wondering what is the actual difference between the above two in java collections API. I am new to java collections API. I know that List is an interface that ArrayList class implements.

Original source