views:

260

answers:

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

Thanks

+5  A: 

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 a LinkedList for example), without affecting the rest of the code. This is will be difficult to do with an ArrayList, not only because you will need to change ArrayList to LinkedList everywhere, but also because you may have used ArrayList specific methods.

You can read about List implementations here. You may start with an ArrayList, but soon afterwards discover that another implementation is more appropriate.

kgiannakakis
can you elaborate on changing the implementation of the List? Using my code as an example, to change myList to a LinkedList, wouldn't one still need to call new LinkedList() on the myList?
kji
oh I commented before your last addition was there..but it all makes sense now..thanks
kji
Yes, and this will be the only code change you will need. Compare with with changing ArrayList to LinkedList in every method. Not to mention having to replaca an ArrayList only method.
kgiannakakis
A: 

I would say that 1 is preferred, unless

  • you are depending on the implementation of optional behavior* in ArrayList, in that case explicitly using ArrayList is more clear
  • You will be using the ArrayList in a method call which requires ArrayList, possibly for optional behavior or performance characteristics

My guess is that in 99% of the cases you can get by with List, which is preferred.

  • for instance removeAll, or add(null)
extraneon
+6  A: 

I am wondering if anyone uses (2)?

Yes. But rarely for a good reason.

Also, how often does the situation actually require using (1) over (2) (i.e. where (2) wouldn't suffice..aside 'coding to interfaces' and best practices etc.)

The "how often" part of the question is objectively unanswerable.

(and can I please get an example)

Occasionally, the application may require that you use methods in the ArrayList API that are not in the List API. For example, ensureCapacity(int), trimToSize() or removeRange(int, int). (And the last one will only arise if you have created a subtype of ArrayList that declares the method to be public.)

That is the only legitimate reason for coding to the class rather than the interface, IMO. (It is possible that you might get a slight improvement in performance ... under certain circumstances ... on some platforms ... but unless you really need that last 0.5%, it is really not worth doing this.)

Stephen C
A better answer. +1
Adeel Ansari
Just for the record: `subList(int, int).clear()` has the same effect as `removeRange(int, int)` and works on all (conforming) `List` implementations.
Joachim Sauer
+2  A: 

(3) Collection myCollection = new ArrayList();

I am using this typically. And only if I need List methods, I will use List. Same with ArrayList. You always can switch to more "narrow" interface, but you can't switch to more "wide".

dart
+1  A: 

The only case that I am aware of where (2) can be better is when using GWT, because it reduces application footprint (not my idea, but the google web toolkit team says so). But for regular java running inside the JVM (1) is probably always better.

Hans Westerbeek
+1  A: 

I think the people who use (2) don't know the Liskov substitution principle or the Dependency inversion principle. Or they really have to use ArrayList.

True Soft