21

Hi I have not a clear idea between,

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

and

ArrayList<String> list = new ArrayList<String>();
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
user3300570
  • 229
  • 1
  • 2
  • 4
  • Well for starters, they are two different types. – Burhan Khalid Jun 02 '14 at 09:22
  • this question is asked earlier, and has an answer: [http://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n][1] [1]: http://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n – user80755 Jun 02 '14 at 09:25
  • In the first type you can change the List implementation to include Vector, Linked list etc instead of the Arraylist. The second type specifically creates a Arraylist and you are stuck with it :) – vikeng21 Jun 02 '14 at 09:27

5 Answers5

14
List<String> list = new ArrayList<String>();

Creates a List of type string, and the list can be potentially typecast into any other type of list. This is called decoupling. You are decoupling your code from a specific implementation of the interface. The advantages it provides, is when writing large amounts of code, you can switch between types of lists to suit your preferences (speed, memory etc), as all of your code, can treat your list as just type List. You can also pass a List as parameters and returns List from functions. And later on if you are not happy with the ArrayList, you just change that one line of code

List<String> list = new ArrayList<String>(); // old code
List<String> list = new LinkedList<String>(); // new code

// The rest of the code doesnt need changing

...
list = getList();
...

public List<String> getList() {
  List<String> temporaryList;
  ...
  return temporaryList;
}

public void changeList(List<string> localListVariable) {}

And your program will behave as expected.


On the other hand,

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

Creates an ArrayList of String types and it cannot be used as any other kind of List (Vector,LinkedList etc). Therefore it is bound by the methods available to an ArrayList. If you now want to change the type of list used, you will have to change all function parameters and return types and so on throughout your program (wherever you have had to create an ArrayList<String> to work with your variable).

Husman
  • 6,819
  • 9
  • 29
  • 47
4

List<String> is a superclass (or could be a valid interface) of ArrayList<String>. Assigning an instance of ArrayList<String> to a List<String> variable is allowed. It's somehow a form of dynamic casting. When accessing List<String> list, only methods accessible with List<String> could be used; and those from ArrayList<String> would be hidden despite the object being an instance of ArrayList<String>.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
2

With respect to the instance in memory that you obtain there is no difference. However, it is considered a good practice to Program to Interfaces (see e.g. What does it mean to "program to an interface"?). Many Java APIs are defined in terms of interfaces, i.e. the functionality will work independently on which implementation of a particular interface you use. This aids code clarity and quality and reduces the probability of bugs which arise from relying on some particular properties of an implementation class (unless these properties are really important).

Community
  • 1
  • 1
Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
1

In both the cases, you create object of ArrayList. But List<String> list = new ArrayList<String>(); will refer the object by using a reference of List<String> whereas ArrayList<String> list = new ArrayList<String>(); will refer the object by using a reference variable of type ArrayList<String>.

For example, you can call a method named ensureCapacity by using a reference of ArrayList<String>, but you can't do that using a reference of List<String>.

The following will compile:

ArrayList<String> list = new ArrayList<String>();
list.ensureCapacity(10);    // This will work

The following won't compile:

List<String> list = new ArrayList<String>();
list.ensureCapacity(10);    // This will not compile
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
  • In either cases you are creating an object of `ArrayList` and not an object of `List`, Although `ArrayList` IS-A `List`. Thus, in this case there won't be any difference between an object of `List` and `ArrayList` – Rahul Bobhate Jun 02 '14 at 10:18
  • Just to complete your answer: you can add cast, then second example will compile as well: List list = new ArrayList(); ((ArrayList) list).ensureCapacity(10); // This will compile. However adding cast is making second example to be actually same as first example. – Nenad Bulatović Dec 10 '16 at 20:51
0

ArrayList creates an object of type ArrayList and List creates an object of type List, ArrayLists underlying interface. Search the javadocs if you dont know what interfaces are.

rhbvkleef
  • 214
  • 3
  • 12