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 Meet the JCF

Jorrell Templeton
Jorrell Templeton
1,563 Points

What are generics?

I don't understand what are generics in Java. HELP!

1 Answer

Simon Coates
Simon Coates
28,694 Points

they let you pass type as a parameter. The most simple instance is something like ArrayList. Being able to set the type lets you have an arrayList of Strings (ArrayList<String>), or an ArrayList of Dates(ArrayList<Date>). They typically contrast this with raw type (accepts any object). You could pass any of a series of objects into an arraylist of objects, but you wouldn't get type protection (during compile) and you'd need to do more casting. Such code would be brittle as passing in an incorrect object type would fail during runtime when casting. (for the record, most explanations of generics delve into an explanation of type issues with raw type - which is pregeneric behaviour. generics are confusing until you see how they used to code before generics were introduced. see https://docs.oracle.com/javase/tutorial/java/generics/why.html )

Moderator Edit: Moved response to Answer section