Recent Tutorials and Articles
    Introduction to Generics in Java
    Published on: 6th August 2013

    This tutorial will explain you the concept of Generics along with a sample program using Generics.

    What are Generics?

    Generics allow you to abstract over types. It enables reference types (classes and interfaces) to be parameters when defining classes, interfaces and methods.

    Generics make it easy to work with container types such as collections, caches etc. It also helps you recognize potential class cast exceptions at compile time. Let's have a look at below sample code without Generics -

    ListUsageWithoutGenerics.java
    package com.sts.allprogrammingtutorials.javabasics;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class ListUsageWithoutGenerics {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		final List list = new ArrayList();
    		list.add("String");
    		
    		//ClassCastException will be thrown on next line
    		Integer integer = (Integer)list.get(0);
    	}
    
    }
    
    

    Above program will compile fine without any errors but if you run it, you will get a ClassCastException saying that String can not be type casted into Integer. This is something which can be detected at compile time itself if you rewrite the same code using Generics.

    ListWithGenerics.java
    package com.sts.allprogrammingtutorials.javabasics;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class ListWithGenerics {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		final List<String> list = new ArrayList<String>();
    		list.add("String");
    		
    		Integer integer = (Integer)list.get(0); // Compile time error
    	}
    
    }
    
    

    In above program, Generics help you define the type of list (in angle brackets) as String and thereby ensuring that -

    1. Only String objects can be added to it.
    2. Only String objects can be retrieved out of it - Thus generating a compile time error as compiler knows that list get method is supposed to return a String which can't be type casted into an Integer.
    Benefits of using Generics
    1. Stronger type checks at compile time: 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.
    2. Elimination of type casts: Consider the following code without generics -

       

      		final List list = new ArrayList();
      	list.add("String");
      	
      	String string = (String)list.get(0);
      	
      	
      Now if we convert the above code into Generics, we no longer need to type cast to String -

       

      		final List<String> list = new ArrayList<String>();
      	list.add("String");
      	
      	String string = list.get(0);
      	
      	
    3. Enabling programmers to implement generic algorithm: By using Generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 6th August 2013

    Comment Form is loading comments...