O'Reilly logo

Java™: A Beginner's Tutorial, Second Edition by Budi Kurniawan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 12. Generics

Generics enable you to write a Java type and create an instance of it by passing a reference type or reference types. The instance will then be restricted to only working with the type(s). For instance, the java.util.List interface is generic. When creating a List object, you can pass a Java type to it and produce a List instance that works only with objects of that type. That is, if you pass java.lang.String, the List instance can only hold String objects; if you pass java.lang.Integer, the instance can only store Integer objects. In addition to parameterized types, you can create parameterized methods too.

The first benefit of generics is stricter type checking at compile time. This is most apparent in the Collections Framework. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required