×

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.


Related Topics

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

Java Math IEEEremainder() Method

The IEEEremainder() method of Math class calculates the remainder as prescribed by the IEEE754 standard. This method simply returns the remainder when f1 (dividend) is divided by f2 (divisor). Syntax: public static...

2 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.

Java Math hypot() Method

The hypot() method of Math class returns the square root for the expression x2 + y2 without the intermediate underflow or overflow . Syntax: public static double hypot(double x, double y) Parameters: The parameters...

2 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.