Is there a difference between the two code snippets, and if so, then what?

So, I can’t understand the difference between these two pieces of code or when someone prefers the other. Fragment # 1

  public static void main(String[] args) {
           List testList = new ArrayList();
           testList.add(new Integer(6));

           String string = (String) testList.get(0);
     }

Fragment # 2

public static void main(String[] args) {
       ArrayList testList = new ArrayList();
       testList.add(new Integer(6));

       String string = (String) testList.get(0);
   }

My interpretation was that in fragment 1, a list named testList is assigned to an ArrayList object. And in fragment 2, an ArrayList named testList is assigned to an ArrayList object. However, that just doesn't make sense to me.

Side question: one of two preferred standards?

+4
source share
2 answers

The first fragment uses an interface type List, the second uses an object type ArrayList.

- ArrayList, , .

+1

List - . ArrayList List. , .

+1

Source: https://habr.com/ru/post/1617140/


All Articles