Category >> Generics  

Why do we need Generic Types in Java?

Generic types are extensively used in Java collections. The fundamental question is why we need Generic Types in Java? Understanding this question will help you better understand related concepts. I will use a short and simple example to show why Generic is introduced to Java.

1. Overview of Generics

The goal of implementing Generics is finding bugs in compilation stage, other than waiting for run-time. This can reduce a lot of time for debugging java program, because compile-time bugs are much easier to find and fix. From the beginning, we need to keep in mind that generic types only exist in compile-time. A lot of misunderstanding and confusion come from this.

2. What if there is no Generics?

In the following program, the “Room” class defines a member object. We can pass any object to it, such as String, Integer, etc.

class Room {
 
	private Object object;
 
	public void add(Object object) {
		this.object = object;
	}
 
	public Object get() {
		return object;
	}
}
 
public class Main {
	public static void main(String[] args) {
		Room room = new Room();
		room.add(60);
		//room.add("60"); //this will cause a run-time error
		Integer i = (Integer)room.get();
		System.out.println(i);
	}
}

The program runs totally fine when we add an integer and cast it. But if a user accidentally add a string “60″ to it, compiler does not know it is a problem. When the program is run, it will get a ClassCastException.

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
	at collection.Main.main(Main.java:21)

You may wonder why not just declare the field type to be Integer instead of Object. If so, then the room is not so much useful because it can only store one type of thing.

3. When generics is used

If generic type is used, the program becomes the following.

class Room<T> {
 
	private T t;
 
	public void add(T t) {
		this.t = t;
	}
 
	public T get() {
		return t;
	}
}
 
public class Main {
	public static void main(String[] args) {
		Room<Integer> room = new Room<Integer>();
		room.add(60);
 
		Integer i = room.get();
		System.out.println(i);
	}
}

Now if someone adds room.add(“60″), a compile-time error will be shown like the following:

We can easily see how this works. In addition, there is no need to cast the result any more from room.get() since compile knows get() will return an Integer.

4. Summary

To sum up, the reasons to use Generics are as follows:

  • Stronger type checking at compile time.
  • Elimination of explicit cast.
  • Enabling better code reusability such as implementation of generic algorithms

Java Generic type is only a compile-time concept. During run-time, all types information is erased, and this is call Type Erasure. Here is an interesting example to show how to avoid the common mistakes of Type Erasure.

References:
1. Why Use Generics?

Category >> Generics  
  • ramya

    as a learner i felt is good

  • release me

    wow man ..you caught it ….nobody would have figure it out ….hats off

  • yfain

    The comment is a little misleading:

    //room.add(“60″); //this will cause a run-time error

    It should be

    //room.add(“60″); //the next line would cause a run-time error