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

Kevin Frostad
Kevin Frostad
3,483 Points

Why do we need to specify the type of the class when making an object of ArrayList?

Wouldn't it be enough to only specify which type the object can hold, like:

ArrayList<String> list = new ArrayList<>()

Instead of:

ArrayList<String> list = new ArrayList<String>()
Kevin Faust
Kevin Faust
15,353 Points

//editted for readability

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Yes your completely right. The first option you have presented is a common way of handling generics. I believe later on in the videos, you will learn that either or is acceptable. Personal preference really

Brendon Butler
Brendon Butler
4,254 Points

Yeah, it's weird. I was always taught to type it out in its entirety, and my IDEs even stopped the code from compiling because of these issues. Things seem to have changed now and my IDEs tell me not to put them in because it's redundant.