Tuesday, 31 January 2017

Generics in Java

There have been many new feature additions in Java over the year, with introduction of lambda expression in Java 8 it has even moved toward functional programming. But long before the lambda expressions and stream API there was one change which had a very big impact on the way you program and that was generics which was added in Java 5.

Generics not only added a new way to declare classes, methods or interface but it also changed many of the classes in the API, Java collections API is an example.

Initially it took some time getting used to the parameters like T, K or V with the classes or methods but now its quite a familiar thing and very much a part of your daily programming.


Generics enable you to specify type parameters when defining classes, interfaces and methods. Here type parameter is the type of data (class or interface) which these classes, interfaces and methods will operate upon.

As example, when you say List<T> that means a list which will store elements of type T. Since you have specified a type parameter so this List class is generic and T is the type parameter which specifies the type of element stored in the List.

Generic class format

A generic class is defined with the following format:

is defined with the following format:

class name<T1, T2, ..., Tn> { 
    /* ... */ 

The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ..., and Tn.

Type Parameter Naming Conventions

By convention, generic type parameter names are single, upper case letters. That helps n distinguishing between type parameters and normal variables.

The most commonly used type parameter names are:

  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

Benefits of Generics

You might be thinking there is already an Object class which provides almost the same thing, as Object is super class of all the other classes so Object class can be used to make a class generic. Though that can be done but it may cause some unseen problems because there is no type safety.

As example -

Let’s say you have a list where you intended to store strings and you have not made it generic that means all its elements will be stored as objects of type Object -

List strList = new ArrayList();
strList.add(new Integer(4));
Iterator itr = strList.iterator();
 String str = (String)itr.next();
 System.out.println("" + str);

Since elements are stored as class Object type so there won’t be any compile time error if you store an integer too (as done in this line strList.add(new Integer(4));), even if you intended to store Strings.

Since the elements are stored as class Object type so you need to cast those elements to String while retrieving. At that time you will get a run time exception when the code will try to cast Integer to String.

So the code snippet shown above will throw run time exception -
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

That brings to the fore couple of benefits of using generics -

  • Stronger type checks at compile time - In the above code if you use generics and specify the parametrized type of List as String you will get compile time error if an attempt is made to add element of any other type to the list. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

    As example -

    List<String> strList = new ArrayList<String>();
    strList.add(new Integer(4));

    With generics this code will throw following compile-time error if you try to add Integer to the list
    The method add(String) in the type List<String> is not applicable for the arguments (Integer)

  • Casting is not required - Another benefit you get is cast is not required. Since you already specify the type with generics so explicit cast is not required.

    As example-

    List<String> strList = new ArrayList<String>();
    Iterator<String> itr = strList.iterator();
        String str = itr.next();
        System.out.println("" + str);

    Here you see casting is not required when retrieving elements from the list.

  • Enabling programmers to implement generic algorithms – In many algorithms, logic remains same, irrespective of the data they are used with. On the other hand, generics make it possible to write classes, methods and interface that work with different kind of data that too in a type safe manner. Now combine these two facts and the result is being able to write generic algorithms that work with any type of data.

    As example-

    Let’s take a simple example to see how to create a generic class with parametrized type. While creating an object of the class you can specify the type, which in generics terminology is called generic type invocation, which replaces T with some concrete value.

    Generic class

    public class GenType<T> {
        T obj;
        public T getObj() {
            return obj;
        public void setObj(T obj) {
            this.obj = obj;

    Using Generic class

    public class GenericDemo {
        public static void main(String[] args) {
            // With Integer type
            GenType<Integer> genInt = new GenType<Integer>();
            int value = genInt.getObj();
            System.out.println("integer value " + value);
            // With String type
            GenType<String> genStr = new GenType<String>();
            genStr.setObj("Generic class test");
            String str = genStr.getObj();
            System.out.println("String value " + str);
            // With Double type
            GenType<Double> genDouble = new GenType<Double>();
            double dblValue = genDouble.getObj();
            System.out.println("Double value " + dblValue);


    integer value 21
    String value Generic class test
    Double value 34.56

    Here you see that the same generic class is first invoked with Integer type, then String and then Double. And you get the added benefit of type safety and no chance of run time exception (which is a probability if you had used an Object class to make the class generic).

Points to remember

  1. Generics provide type safety as there are stronger type checks at compile-time.
  2. Generics don’t work with primitive types, type arguments passed to the type parameters must be references.
  3. You can have generic classes, generic methods and generic interfaces.
  4. Type parameters are erased when generic classes are compiled.
  5. A generic class with type parameters can’t have static members using the type parameter. As example -
    public class GenType<T> {
        static T obj;
    This code will have compile-time error - Cannot make a static reference to the non-static type T.
  6. In Java 7 and later you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond.
    As example - List<Integer> ls = new ArrayList<>();
    With arraylist you don’t need to write <Integer> again, empty set of type argument (<>) will do, type will be inferred automatically.

That's all for this topic Generics in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Bounded type parameter in Java generics
  2. Generic class, interface and generic method
  3. Wildcard in Java Generics
  4. varargs in Java
  5. covariant return type in Java
  6. Parallel Stream in Java Stream API

You may also like -

>>>Go to Java advance topics page

No comments:

Post a Comment