Skip to content
← Back to Community
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 ();

Answered by hellooneplus [earned 5 cycles]
View Answer
Voters
Profile icon
zal9affar
Profile icon
ShadeVI
Comments
hotnewtop
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.

Profile icon
Coder100

Difference

List is abstract, so you can change it's 'type'. ArrayList is not, but it will allow you more speed if you know all your types are going to be the same.

Which one to use?

I have always used ArrayList, and have never found any need for List.
https://java2blog.com/java-list-vs-arraylist/

Profile icon
RYANTADIPARTHI

Solution

the list in java is not dynamic, Meaning it can hold only a certain amount on information, based on what you put. So whatever value you want it to hold, that's all it can. While the array list is a dynamic. You don't have to specify anything. You can add how much you want. the internal process just makes more, and more space foryou to add unlimited.

Profile icon
ShadeVI

@RYANTADIPARTHI
thanks, but in the first example i still don't understand why the declaration type is List and the implementation is ArrayList<>. This is a list or an arrayList?

Profile icon
RYANTADIPARTHI

@ShadeVI
well it's just like that. they have differences. this is a arraylist btw. What you are showing is an arraylist.

Profile icon
Coder100

no, you're wrong, arrays have stsatic length, but list and arraylist have dynamic length

@RYANTADIPARTHI