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:

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

Generics Class Examples

FileName: GenericsExample.java

Output:

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

Output:

Generics Methods in Java

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

FileName: GenericsExample2.java

Output:

Advantages of Generics in Java

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

FileName: GenericsExample3.java

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

Output:

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

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

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

Output:

FileName: GenericsExample8.java

Output:

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.

Pin It on Pinterest

Share This