Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures 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>; ?