Java Generics Tutorial

12 Flares Twitter 0 Facebook 5 Google+ 7 Pin It Share 0 LinkedIn 0 12 Flares ×

This tutorial  looks at Java Generics in detail and explores the flexibility it offers. Most important aspects of Generics are covered in Simple to understand language. Hope this tutorial is useful to readers.

Java Generics

 Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Type parameters provides a way to re-use the same code with different input types.

Generics was first introduced in the Java Collections framework. Collections accept Objects as the input and this means you can create a a collection of Strings , Integers or any other object. Generally you create a collection containing the same type.

For example, you can create an ArrayList containing Strings. For some reason, if you accidentally add another type say an Integer to this ArrayList, it will not complain and the issue will be discovered only during the run-time, when you itereate the ArrayList and type cast it to a String. This was a common cause of avoidable errors and generics was introduced in Java Collections to Solve these types of errors.

With the Introduction of Generics you now have the flexibility of declaring the type of ArrayList at compile time and when you add any other type to this ArrayList, compiler will throw an error. This means now you have ensured type safety at compile time.

Generics way of declaring an ArrayList

Non Generic way

List list = new ArrayList();

 Advantages of Generics

1)  Compile time type safety

2) No need to Cast : Since we declare the type, there is no need to cast as the JVM knows already what type the collection has.

3) Enables programmers to implement generic algorithms or methods

 Generics Definition

A generic class is defined with the following format:

class name<t1, t2,=”” …,=”” tn=””> { }

The type parameter section, delimited by angle brackets (<>), follows the class name. Let us look at an example from the Java library it self.

When a generic type is instantiated, it is called Parameterized type. For instance see how we instantiate a parametrized String ArrayList

Bounded Types

Above example of List is an example of unbounded type ie the ArrayList is able to take an Object as the parametrized type. There are situations where we want to restrict the type to be of a particular class. This what the bounded type parameters are for.

The above Calculator class is not bounded as it accepts all Objects. We want to restrict this to only include Numbers. As you know Number is the the super class of Integer, Float, Double, Long etc. We want our Calculator to only accept Numbers. By doing this we can make the Calculator a Bounded Generic Type.

Here we have explicitly mentioned that the “Type should be a Number”

 Multiple bounds

Generics also lets you declare multiple bounds. This means you can say that the particular class accepts types of A, B, Or C.

 Generics and SubTyping

Understanding the Subtype and Super type relationship is a very important and confusing concept. Understanding this clearly makes you better equipped to work with generics.

Let us consider the ArrayList example

Here we have declared an ArrayList of type Integer and assigned that list to a List of type Number. Our argument is – “a List of  Integer is indeed a List of Number”
But this is not correct !.

This means List of type Integer  is a not sub type of List Number in the Generics World.


Wild Cards – Generics

There are certain situations where we want to accept all kinds of  Collections ( Say all types of ArrayList , be it an ArrayList<Integer> , ArrayList<String> or anything else). How do we write a such a method. What is the supertype of all kinds of List ? It’s written List<?> (pronounced “List of unknown”), that is, a List whose element type matches anything. It’s called a wildcard type


Oracle- Java Generics Tutorial