Intro to Java Generics

This entry is part 8 of 13 in the series Intro to Java

To me it seems a better word would be Specifics. The use of Generics is applying a specific type to a class that can be used in a generic way. One of the most common generic classes is List. A list allows you to put any type of Object in to it. In fact, you can put several different types of Objects into a List. But, there is a way to create a specific List type that can only take one type of Object.

What do they look like?

Since we are talking about Lists, lets look at what a ArrayList would look like that could only take Strings.

List aList = new ArrayList();

What goes between the <> is the type that you want to specify for the List. You need to do it in the left side declaration, and on the right side where you are creating the Object.
Continue reading