Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

ArrayList vs. List

What's the difference between ArrayList and List. Why do Craig always write: List<String> name = ArrayList<String>();

Why not write: ArrayList<String> name = ArrayList<String>();

Then you don't have to import the List util, right?

2 Answers

Hi Cecilie,

There's no difference between list implementations in both of your examples. There's however a difference in a way you can further use variable myList.

When you define your list as:

List myList = new ArrayList();

you can only call methods and reference members that belong to List class. If you define it as:

ArrayList myList = new ArrayList();

you'll be able to invoke ArrayList specific methods and use ArrayList specific members in addition to those inherited from List.

Nevertheless, when you call a method of a List class in the first example, which was overridden in ArrayList, then method from ArrayList will be called not the one in the List.

That's called polymorphism.

Hope this helps!

Nicolas Hampton
Nicolas Hampton
44,725 Points

Cecilie,

I actually remembered this from java and thought it was an interface issue, so I did a little digging and found an answer that made sense to me in any language:

"Originally posted by Ernest Friedman-Hill on http://www.coderanch.com/t/379305/java/java/List-ArrayList : This is called "programming to interfaces." If you write your whole program this way -- including method arguments of type List instead of ArrayList, and return types of List instead of ArrayList -- then you're free to change to any other implementation of List by changing only one line of code. As different implementations have different performance characteristics, this flexibility is a good thing.

Looking at this more broadly, you can and should use this with your own classes as well. Keeping your subsystems isolated via interfaces helps make them more robust and flexible. It also helps immensely in testing: being able to substitute different types makes it easier to test individual classes in isolation."

And he's right. Keeping the type as simple as possible makes it less likely that a type error will be thrown in a random place later. That could happen if you're returning strange list types in some places and not others. Looks like a best practice worth keeping. Thanks for making me look it up, and happy coding!

Nicolas