Ask coding questions

← Back to all posts
Java List and ArrayList
ShadeVI (6)

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

List <String> wordlist = new ArrayList <> ();


ArrayList <String> wordlist = new ArrayList <String> ();

hellooneplus (0)

The main differences between List and ArrayList are given below List is an interface while ArrayList is a class.
List <String> 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.

Hope this will clear yours doubt.

Coder100 (18874)


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.



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.

ShadeVI (6)

@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?


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

Coder100 (18874)

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