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.
In your statement 1, since you are referring
mylistasList<Integer>while it is stillArrayList<Integer>, hence you can use the methods available in theListinterface 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 ofListe.g.LinkedList<Integer>or your custom implementation class.Your second statement, creates and references the object as
ArrayList<Integer>ONLY. Some people perefer it whenmylistis to be used locally in the method.