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 trialKevin Frostad
3,483 PointsWhy 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>()
1 Answer
Kevin Faust
15,353 PointsYes 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
Kevin Frostad
3,483 PointsThanks :)
Brendon Butler
4,254 PointsYeah, 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.
Kevin Faust
15,353 PointsKevin Faust
15,353 Points//editted for readability