Skip to content
Java List and ArrayList
Profile icon
ShadeVI

Hi guys,
is there any difference between these two ways?

List wordlist = new ArrayList <> ();

and

ArrayList wordlist = new ArrayList ();

You are viewing a single comment. View All
Profile icon
hellooneplus

The main differences between List and ArrayList are given below List is an interface while ArrayList is a class.
List wordlist = new ArrayList <> ();
In the above line, we are assigning the ArrayList class object to the List interface.

There is a big plus on declaring them using the interface, which is what is known as "coding to an interface" instead of "coding to an implementation" which is a big Object-Oriented Design (OOD) principle.

  1. Declaring them using an interface, means you can later substitute that value with any other concrete class that implements that interface, instead of being locked into that particular concrete class

  2. You can take full advantage of polymorphism by declaring them using an interface, because each implementation can call the correct method at runtime.

  3. You follow the OOD principle of code to an interface.

https://javahungry.blogspot.com/2017/11/arraylist-in-java-tutorial-with-example.html

Hope this will clear yours doubt.