21

Hi I have not a clear idea between,

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

and

ArrayList<String> list = new ArrayList<String>();
3
  • Well for starters, they are two different types. Commented Jun 2, 2014 at 9:22
  • this question is asked earlier, and has an answer: [stackoverflow.com/questions/9852831/… [1]: stackoverflow.com/questions/9852831/… Commented Jun 2, 2014 at 9: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 :) Commented Jun 2, 2014 at 9:27

5 Answers 5

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).

Sign up to request clarification or add additional context in comments.

Comments

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>.

Comments

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).

Comments

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

2 Comments

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
Just to complete your answer: you can add cast, then second example will compile as well: List<String> list = new ArrayList<String>(); ((ArrayList<String>) list).ensureCapacity(10); // This will compile. However adding cast is making second example to be actually same as first example.
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.

2 Comments

Is object of ArrayList take more memory space than object of List?
You shouldn't worry about that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.