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

What's the difference between declaring the list as List<String> newList and ArrayList<String> newList = ...?

What's the difference between declaring the list as List<String> newList and ArrayList<String> newList = ...?

I'm sure you can declare it both ways, but I'm not sure what difference does it make.

1 Answer

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

Hey, Yani! List is an Interface which has parameters and methods in it that classes have to implement in order to implement the interface. ArrayList is a class that implements the List interface. You should not be able to insatiate a new List<E>() because it is only an interface and not a fully developed class; however, you could declare a variable as a List<E> and then instantiate it as an new ArrayList<E>(). This is an example of polymorphism. There's a good article explaining what polymorphism is and its benefits and limitations here: http://www.artima.com/objectsandjava/webuscript/PolymorphismInterfaces1.html.

Does that help? If not, I can try to explain a little further. :)

Thanks Walter! That was very helpful. :)

Walter Allen
seal-mask
.a{fill-rule:evenodd;}techdegree
Walter Allen
iOS Development with Swift Techdegree Student 16,023 Points

My pleasure, Yani! If it answered your question, do me a favor and mark it as "Best Answer." If not, let me know and I'll develop some more. :) Happy coding!

OK, but why he didn't type ArrayList<String> newList = new ArrayList<String>; ?