As a Java developer, it is very important to know what are the generics, its advantage, and when and how to use them.

What are Generics?

Generics are a facility of generic programming that were added to the Java programming language in 2004 within version J2SE 5.0.

What are the advantages of Generics?

Basically, there are 3 benefits of the Generics. They are

  1. Stroger Type check at the compile Time (Type Safety)
  2. Elimination of the Casting
  3. Enable Programmers to implement Generic Algorithm

Case I: Type Safety

To prove this case, We will first take an Array and how it enables type safety. Let’s suppose we have a programming requirement of holding a list of Strings. so, for this, we are using an Array. We can define an array in java like this:

String[] s= new String[100];

s[0] =”John”;

s[1] = “Tina”;

s[2] = new Integer[100];

In the above array, insertion at s[0] and s[1] will not throw any exceptions but at s[2] it will throw a compile-time exception. It is because arrays are type-safe and it guarantees the type of data it holds.

but, In contrast to this, Collections are not type-safe. we cannot guarantee the type of data that is present in the collection. Let’s suppose our programming requirement is to hold the String data and we define an ArrayList. This is how it will be:

ArrayList al = new ArrayList();

al.add(“dog”);

al.add(“donkey”);

al.add(new Integer(10));

There will be no compile-time error and so while retrieving the data we will try something like this:-

String animal1 = (String) al.get(0);

String animal2 = (String) al.get(1);

String animal3 = (String) al.get(2);

Now, on animal3 there will be runtime-exception saying : java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String

Hence, we can see that collections are not type-safe.

How do Generics provide type safety?

Let’s suppose we want to create a list that can only hold Strings. so for that, we can create ArrayList like this:

ArrayList<String> al = new ArrayList<String>();

al.add(“Mouse”);

al.add(1); //compile error as al only accepts Strings

if we try to add any other data type, apart from String, the Compiler will throw compile error.

Case II: Elimination of Type Casting

Let’s take the same example given above:-

ArrayList al = new ArrayList();

al.add(“dog”);

al.add(“donkey”);

String animal1 = (String) al.get(0);

String animal2 = (String) al.get(1);

for the above example, while retrieving data from the list, Type Casting is required.

String animal1 = (String) al.get(0);

String animal2 = (String) al.get(1);

but when we define the type of the collection by using generics this typecasting while retrieving data is not mandatory. for instance, if we define the same above code as :

ArrayList<String> al = new ArrayList<String>();

al.add(“dog”);

al.add(“donkey”);

We can simply retrieve data without typecasting.

String animal1 = al.get(0);

String animal2 = al.get(1);

Finally, Let’s differentiate the generic and non-generic versions of an object by using an ArrayList.

Hence, We can conclude the main objectives of generics are:-

  1. To Provide Type Safety
  2. To resolve Type casting problems