ArrayList List Assignment

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

+5
source share
6 answers

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.

+3
source

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

+2

, .

, , List, , , .

+2

, List , , , , , List<K>, , ArrayList LinkedList.

+2

. .

+2

The second example Program to Interfaceand its preferred path.
For more information. What does "interface programming" mean?

+2
source

All Articles