Is there a difference between the two lines in the following
ArrayList<String> arrName = new ArrayList<String>(); List<String> arrName = new ArrayList<String>();
thanks for the answer
Almost always the second is preferable to the first. The second advantage is that the implementation Listcan change (for example, by LinkedList) without affecting the rest of the code. It will be hard to do with ArrayListnot only because you will need to change ArrayListto LinkedListeverywhere, but also because you can use special techniques ArrayList.
List
LinkedList
ArrayList
The latter is usually recommended until you only need an interface List. This is called "programming for an interface, not an implementation."
, stackoverflow: "C c = new C()" "A c = C()" C A Java
, .
, , List, , , .
, List , , , , , List<K>, , ArrayList LinkedList.
List<K>
. .
The second example Program to Interfaceand its preferred path.For more information. What does "interface programming" mean?
Program to Interface