(1) List<?> myList = new ArrayList<?>();
(2) ArrayList<?> myList = new ArrayList<?>();
I understand that with (1), implementations of the List interface can be swapped. It seems that (1) is typically used in an application regardless of need (myself I always use this).
I am wondering if anyone uses (2)?
Also, how often (and can I please get an example) does the situation actually require using (1) over (2) (i.e. where (2) wouldn't suffice..aside coding to interfaces and best practices etc.)
Out of the following two:
First is generally preferred. As you will be using methods from
List
interface only, it provides you the freedom to use some other implementation ofList
e.g.LinkedList
in future. So it decouples you from specific implementation. Now there are two points worth mentioning:ArrayList
overLinkedList
. More here.Yes sometimes (read rarely). When we need methods that are part of implementation of
ArrayList
but not part of the interfaceList
. For exampleensureCapacity
.Almost always you prefer option (1). This is a classical design pattern in OOP where you always try to decouple your code from specific implementation and program to the interface.
List is an interface.It doesn't have methods. When you call method on a List reference. It in fact calls the method of ArrayList in both cases.
And for future you can change
List obj = new ArrayList<>
toList obj = new LinkList<>
or other type which implements List interface.I would say that 1 is preferred, unless
My guess is that in 99% of the cases you can get by with List, which is preferred.
removeAll
, oradd(null)
Actually there are occasions where (2) is not only preferred but mandatory and I am very surprised, that nobody mentions this here.
Serialization!
If you have a serializable class and you want it to contain a list, then you must declare the field to be of a concrete and serializable type like
ArrayList
because theList
interface does not extendjava.io.Serializable
Obviously most people do not need serialization and forget about this.
An example:
Almost always the first one is preferred over the second one. The first has the advantage that the implementation of the
List
can change (to aLinkedList
for example), without affecting the rest of the code. This will be a difficult task to do with anArrayList
, not only because you will need to changeArrayList
toLinkedList
everywhere, but also because you may have usedArrayList
specific methods.You can read about
List
implementations here. You may start with anArrayList
, but soon afterwards discover that another implementation is more appropriate.When you write
List
, you actually tell, that your object implementsList
interface only, but you don't specify what class your object belongs to.When you write
ArrayList
, you specify that your object class is a resizable-array.So, the first version makes your code more flexible in future.
Look at Java docs:
Class
ArrayList
- Resizable-array implementation of theList
interface.Interface
List
- An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted.Array
- container object that holds a fixed number of values of a single type.