Generics in Java

Generics in Java

Parameterizedtypes mean generic. Generics allow types (Character, Integer, String, …, etc., as well as user-defined types) to act as parameters to interfaces, classes, and methods. Generics in Java facilitate the creation of classes that deal with different types of datatypes. Entities, such as methods, interfaces, classes that act on the parameterized type,are known as the generic entity. Generics is similar to template C++ templates.

Need of Generics in Java

We know that theObject class is the parent class of all the Java classes, and hence,it is obvious that the Object reference can refer to any object. These features do not ensure type safety.To achieve the same in Java, we use Generics.

Generics Class in Java

Similar to C++, we use angular brackets (<>) to mention parameter types in the creation of the generic class.

Syntax:

 // For creating an object of a generic class
GenericClass<T> obj = new GenericClass<T>();
Where T refers to parameter type such as String, Character, Integer, etc. 

Note: In the parameter,use of primitive type is not allowed. For example, 'char', 'int', 'float', 'double'

Generics Class Examples

FileName: GenericsExample.java

 // A Java program that shows the working of the user-defined
// Generic class
// We use <> to specify Parameter type
class ABC<Type>
{
// Declaring an object of type "Type"
Type obj;
// constructor of the class ABC
ABC(Type obj)
{
    this.obj = obj;
}
// returning the object
public Type getObject()
{
return this.obj;
}
}
public class GenericsExample
{
// main method
public static void main (String argvs[])
{
// An instance of the Integer type
ABC <Integer> intObj = new ABC<Integer>(17);
System.out.println("The value of the integer is: " + intObj.getObject());
// An instance of the String type
ABC <String> strObj = new ABC<String>("Tutorial & Example");
System.out.println("The value of the string is: " + strObj.getObject());
// An instance of the Float type
ABC <Float> floatObj = new ABC<Float>(9.9f);
System.out.println("The value of the float is: " + floatObj.getObject());
// An instance of the Character type
ABC <Character> charObj = new ABC<Character>('E');
System.out.println("The value of the char is: " + charObj.getObject());
}
} 

Output:

 The value of the integer is: 17
The value of the string is: Tutorial & Example
The value of the float is: 9.9
The value of the char is: E 

Explanation:The above-written code showshow easy it is to create instances of different datatypes with the help of the user-defined Generic class ABC.

Let’s see another example of Java generic.

FileName: GenericsExample1.java

 // A Java program that shows the working of the user-defined
// Generic class having multiple parameters
// We use <> to specify Parameter type
class ABC<U, T>
{
// A reference of type U
U uObj;
// A reference of type T
T tObj;
// constructor of the class ABC
ABC(U obj1, T obj2)
{
    this.uObj = obj1;
    this.tObj = obj2;
}
// For printing the objects of types T and U
public void print()
{
    System.out.println(uObj);
    System.out.println(tObj);
}
// a method for returning object of type T
public T getTObj()  { return tObj; }
// a method for returning object of type U
public U getUObj()  { return uObj; }
}
// Driver class
public class GenericsExample1
{
// main method
public static void main (String argvs[])
{
// An instance of the Integer, Character type
ABC <Integer, Character> intCharObj = new ABC<Integer, Character>(10, 'Q');
System.out.println("The value of the integer is: " + intCharObj.getUObj());
System.out.println("The value of the character is: " + intCharObj.getTObj());
// An instance of the String, Float type
ABC <String, Float> strFloatObj = new ABC<String, Float>("Tutorial & Example", 78.90f);
System.out.println("The value of the string is: " + strFloatObj.getUObj());
System.out.println("The value of the float is: " + strFloatObj.getTObj());
// An instance of the Character, Float type
ABC <Character, Float> charFloatObj = new ABC<Character, Float>('V', 7.09f);
System.out.println("The value of the character is: " + charFloatObj.getUObj());
System.out.println("The value of the float is: " + charFloatObj.getTObj());
}
} 

Output:

 The value of the integer is: 10
The value of the character is: Q
The value of the string is: Tutorial & Example
The value of the float is: 78.9
The value of the character is: V
The value of the float is: 7.09 

Generics Methods in Java

Similar to Generic Classes,Java allows us to create generics methods. Observe the following program.

FileName: GenericsExample2.java

 // A Simple Java program to show working of user defined
// Generic functions
public class GenericExample2
{
// A Generic method example
public <T> void display (T ele)
{
    System.out.println(ele.getClass().getName() + " = " + ele);
}
// main mehtod
public static void main(String argvs[])
{
    // creating an instance of the class GenericExample2
    GenericExample2 obj = new GenericExample2();
    // invoking the generic method with the Integer argument
    obj.display(151);
    // Calling generic method with String argument
    obj.display("Tutorial & Example");
    // Calling generic method with float argument
    obj.display(1.51f);
    // Calling generic method with double argument
    obj.display(1.67);
}
} 

Output:

 java.lang.Integer = 151
java.lang.String = Tutorial & Example
java.lang.Float = 1.51
java.lang.Double = 1.67 

Advantages of Generics in Java

1) Generics ensure type safety:Generics ensure type safety through various ways.  Observe the following code.

FileName: GenericsExample3.java

 // A basic Java program to demonstrates
// the working of the user-defined
// Generic class
// We use <> to specify Parameter type
class ABC<P>
{
               // An object of type P is declared
               P obj;
               ABC(P obj) { this.obj = obj; } // constructor
               public P getObject() { return this.obj; }
}
// Driver class for doing
// the testing of above
public class GenericsExample3
{
// main method
public static void main (String argvs[])
{
               // an instance of the Integer type
               ABC <Integer> intObj = new ABC<Integer>(105);
               System.out.println("The value of the integer is" + intObj.getObject());
               // an instance of the String type
               ABC <String> strObj = new ABC<String>("Tutorial & Example");
               System.out.println("The value of the string is " + strObj.getObject());
               intObj = strObj; // it results in an error
}
} 

Output:

Explanation:Even thoughintObj and strObj are the objects of the same class ABC, they represent the difference in parameter type. Hence, the error occurs, which shows that Generics ensures type safety.

Not only this, but Generics also helps to make errors appear at the compile time. Supposethere is an ArrayList (without Generic) that stores the person’s name, and if someone stores a number by mistake, this mistakeignored by the compiler. Observe the following code.

FileName: GenericsExample4.java

 // A Simple Java program to demonstrate that NOT using
// generics may cause run time exceptions
// importing ArrayList
import java.util.ArrayList;
public class GenericsExample4
{
    // main method
    public static void main(String argvs[])
    {
        // Creating an ArrayList that has not specified any type
        ArrayList personNames = new ArrayList();
        personNames.add("Rahul Dravid");
        personNames.add("Virender Sehwag");
        personNames.add("Sachin Tendulkar");
        personNames.add("Zaheer Khan");
        personNames.add("Virat Kohli");
        personNames.add("Anil Kumble");
        personNames.add(12); // Compiler accepts it without any issue
        // size of the array list
        int size = personNames.size();
        // printing the ArrayList elements
        for(int i = 0; i < size; i++)
        {
            System.out.println(personNames.get(i));
        }
    }
} 

Output:

 Rahul Dravid
Virender Sehwag
Sachin Tendulkar
Zaheer Khan
Virat Kohli
Anil Kumble
12 

Explanation:Till this point, everything seems fine.However, it should not be fine, as 12 should not be entertained in the person’s name list. The above code is potent to throw an exception or error. Supposethe next task is to find the length of strings present in the list, then at that time, we will face issues. Observe the following code.

FileName: GenericsExample5.java

 // A Simple Java program to demonstrate that NOT using
// generics may cause run time exceptions
// importing ArrayList
import java.util.ArrayList;
public class GenericsExample5
{
// main method
public static void main(String argvs[])
{
    // Creating an ArrayList that has no any type specified
    ArrayList personNames = new ArrayList();
     personNames.add("Rahul Dravid");
     personNames.add("Virender Sehwag");
     personNames.add("Sachin Tendulkar");
     personNames.add("Zaheer Khan");
     personNames.add("Virat Kohli");
     personNames.add("Anil Kumble");
     personNames.add(12); // Compiler accepts it without any issue
// size of the array list
    int size = personNames.size();
    // printing the ArrayList elements
    for(int i = 0; i < size; i++)
    {
        System.out.println(personNames.get(i));
    }
    // for storing the length of strings
    int len[] = new int[size];
    // loop for finding the string length
    for(int i = 0; i < size; i++)
    {
        len[i] = ((String)personNames.get(i)).length();
    }
}
} 

Output:

Explanation:We observe thatthe program did not get terminated normallybecause of the presence of number 12 in the list of strings. It raised the ClassCastException. In order to avoid such exceptions, it is better to catch such issues during the compile time. In fact, it is always better to address as many issues as one can during compile-time. It is because compile-time issues are much easier to handle as compared to runtime issues, such as exceptions. To fix this issue during compile time, one has to use generics. The following program shows the same.

FileName: GenericsExample6.java

 // A Simple Java program to demonstrate that using
// generics,one can catch issues during the compile time
// importing ArrayList
import java.util.ArrayList;
public class GenericExample6
{
// main method
public static void main(String argvs[])
{
    // Creating an ArrayList of type specified as String
    ArrayList<String> personNames = new ArrayList<String>();
     personNames.add("Rahul Dravid");
     personNames.add("Virender Sehwag");
     personNames.add("Sachin Tendulkar");
     personNames.add("Zaheer Khan");
     personNames.add("Virat Kohli");
     personNames.add("Anil Kumble");
     personNames.add(12); // now, the compiler will not allow
    // size of the array list
    int size = personNames.size();
    // printing the ArrayList elements
    for(int i = 0; i < size; i++)
    {
        System.out.println(personNames.get(i));
    }
    // for storing the length of strings
    int len[] = new int[size];
    // loop for finding the string length
    for(int i = 0; i < size; i++)
    {
        len[i] = ((String)personNames.get(i)).length();
    }
}
} 

Output:

Explanation:Now, we get the compilation error, as the compiler is not allowing 12 in the String array list, which is better as we do not have to wait for the last loop to find the length of the string to get the exception. Also, the ArrayList is ensuring type safety as it is not allowing the number 12 in its list. Thus, type safety is also ensured.

2) In Generics, typecasting is not required: Generics allows us to specify the parameter type. Therefore, only elements of that type are entertained in the list, not the other one; hence typecasting is not required. In the absence of generics, one has to do the type casting explicitly. Consider the following programs.

FileName: GenericsExample7.java

 // A Simple Java program to demonstrate that type casting
// is required when generics is not used.
// importing ArrayList
import java.util.ArrayList;
public class GenericsExample7
{
    // main method
    public static void main(String argvs[])
    {
        // Creating an ArrayList that has not specified any type
        ArrayList personNames = new ArrayList();
        personNames.add("Rahul Dravid");
        personNames.add("Virender Sehwag");
        personNames.add("Sachin Tendulkar");
        personNames.add("Zaheer Khan");
        personNames.add("Virat Kohli");
        personNames.add("Anil Kumble");
        // size of the array list
        int size = personNames.size();
        // printing the ArrayList elements
        for(int i = 0; i < size; i++)
        {
             // typecasting into string
             String name = (String)personNames.get(i);
             System.out.println(name);
        }
    }
} 

Output:

 Rahul Dravid
Virender Sehwag
Sachin Tendulkar
Zaheer Khan
Virat Kohli
Anil Kumble 

FileName: GenericsExample8.java

 // A Simple Java program to demonstrate that type casting
// is not required when generics is used.
// importing ArrayList
import java.util.ArrayList;
public class GenericsExample8
{
    // main method
    public static void main(String argvs[])
    {
        // Creating an ArrayList that has not specified any type
        ArrayList<String> personNames = new ArrayList<String>();
        personNames.add("Rahul Dravid");
        personNames.add("Virender Sehwag");
        personNames.add("Sachin Tendulkar");
        personNames.add("Zaheer Khan");
        personNames.add("Virat Kohli");
        personNames.add("Anil Kumble");
        // size of the array list
        int size = personNames.size();
        // printing the ArrayList elements
        for(int i = 0; i < size; i++)
        {
             // type casting not required!
             String name = personNames.get(i);
             System.out.println(name);
        }
    }
} 

Output:

 Rahul Dravid
Virender Sehwag
Sachin Tendulkar
Zaheer Khan
Virat Kohli
Anil Kumble 

3) Reusability of Code:By creating a generic class or method, one can use different parameter types on the same generic method. Thus, writing code explicitly for different types is not required. The topmost example of in this section shows the same.