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 Java Data Structures Exploring the Java Collection Framework Lists

Awais Jamil
Awais Jamil
7,539 Points

didn't understood why Craig used List<String> on the decleration and new ArrayList<String> on the Right ?

didn't understood why Craig used List<String> on the decleration and new ArrayList<String> on the Right ?

1 Answer

Steven Stanton
Steven Stanton
59,998 Points

Hi Awais,

List is an interface, so it cannot be instantiated directly (you can't type new List() because the compiler wont know which implementation you are talking about). ArrayList is a class, and is a pretty standard implementation of the List interface.

As I understand it, the advantage of referring to this ArrayList as a List everywhere else in the code, is that it makes it easier to swap your implementation later on to some other kind of List (e.g. a LinkedList) - you would only need to change that one line where you create it.

Hope that makes some sense.

Gotta say I was thinking about the same issue as Awais, thank you for your answer Steven!!