Java Generics
Omar Ismail
Senior Software Engineer @ Digitinary | Java 8 Certified? | Spring & Spring Boot?????? | AWS? | Microservices ?? | RESTFul Apis & Integrations ?? FinTech ?? | Open Banking ?? | Digital Payments and Transformation??
Suppose we have to make a list of human beings living in society. It doesn’t matter whether it’s a child, teen or adult. All that matters is they should be human. In such cases, we will not categorize them and will group them as a “Human Being”.
Similarly in Java when we store data we focus on the content and not datatype and that’s where Generics are used. Java Generics is a programming-language feature that allows for the definition and use of generic methods and generic types.
Java Generics
Java introduced the concept of Generics since Java 5 (J2SE 5) to deal with compile-time type checking and removing the risk of?ClassCastException?that was common while working with collection classes. Generics in Java is one of the most important features introduced since Java 5.
The term Generics in Java represents a set of features in a language, that relates to defining and using the generic methods and types. In Java, Generic methods and types are different from regular methods and types. They differ from each other as generic methods have type parameters.
We can see Java Generics as the?templates in C++. Using Java Generics, we can use the wrapper classes like Integer, String, Double, etc and user-defined types as the parameters to classes, methods, and interfaces.
We can utilize the generics for any kind. For example,?classes like HashSet, ArrayList, HashMap, etc use the Java Generics very well.
Need for Java Generics
Java Generics allow us to write a single method that could be able to perform operations in various types of objects that support that method.
Using Java Generic classes and methods, programmers can specify a set of related methods with a single/generic method declaration, or with a single class declaration.
For example, the Java Generics concept allows us to write a generic method for sorting an array of different types of objects, like to invoke the generic method with Character arrays, Integer arrays, String arrays, Double arrays and so on to sort the array elements.
Moreover, Java Generics provide compile-time type safety that allows the programmers to catch invalid types or faults during compilation.
Java Generic Classes
A generic class is a class that can refer to any type. To create a generic class of a specific type, we put the T type parameter. The angular brackets?<>?are used to specify parameter types in Java generic class creation.
Let’s discuss a simple example to create and use the generic class.
Creating a Generic Class
The declaration of a generic class is similar to a non-generic class declaration, the only difference is that the generic class name is followed by a type parameter section. The following code shows the creation of a generic class.
Here, the type T indicates that it can refer to any type of class like Integer, String, Double, Character, and Employee, etc. The specified type of class will store and retrieve the data of the same type.
Note:?In Parameter type, we cannot use primitives data types like ‘int’,’char’ or ‘double’, etc.
Using a Generic Class
Output:
18
Code to understand Generic Classes:
Output:
15
TechVidvan
Using Multiple type Parameters in Generic Classes
We can also use multiple parameters of different types in a generic class, that is, the parameter type section of a generic class can have more than one type of parameter separated by commas.
These classes are known as?parameterized classes?since they accept more than one parameter.
Code to illustrate multiple type Parameters in Generic Classes:
Output:
String value: TechVidvan
Integer value: 15
Type Parameters
The type parameter’s naming conventions are crucial for learning generics thoroughly. The common type parameters are as follows:
Java Generic Methods
We can declare a single generic method and we can call this method with arguments of different types. The compiler handles each method call appropriately according to the types of the arguments passed to the generic method.
Rules to define Generic Methods
Code to understand Generic Methods:
Output:
Array integerArray contains:
10 20 30 40 50
Array doubleArray contains:
1.2 2.5 4.6 7.8
Array characterArray contains:
T e c h V i d V a n
What is not allowed to do with Java Generics?
Now, we will discuss some tasks that are not allowed to do in Java Generics. So let’s examine each of them.
a) You can’t have a static field of type
In your generic class, you can not define a static generic parameterized member. Any attempt to do so will generate a compile-time error. The error will be like: Cannot make a static reference to the non-static type T.
b) You can not create an instance of T
We can also not create an object of T. Any attempt to do so will fail with an error: Cannot instantiate the type T. For example,
c) We can’t use primitive data types with Generics declaration
We can’t declare generic expressions like List or Map <int, double>. But, we can use the?wrapper classes?in place of primitive data types and then use the primitives while passing the actual values. Auto-boxing converts these primitive types to their respective wrapper classes.
d) You can’t create Generic exception class
We can’t pass an instance of generic type along with exception being thrown. This is not allowed in Java. For example, the following line causes an error.
When you try to do this, you will get an error message like this: The generic class?GenericException?may not subclass?java.lang.Throwable.
Advantages of Java Generics
Applications that make use of Java Generics have several benefits over the non-generic code. Some of them are as follows –
1. Code Reuse
We can create a generic strategy or a class or an interface once and use it for any type we need and for any number of times.
2. Sort Safety
It is better to know the faults and issues in your code at compile-time rather than at run-time. Java Generics enables you to detect the faults at compile-time than at runtime.
Suppose, you need to make an ArrayList that stores the name of undergraduate students and if by mistake, software engineer includes an integer in place of a string, compiler permits it. However, when we try to gain this information from ArrayList, it causes issues at runtime.
3. Individual Type Casting isn’t required
In the above case, if we don’t use Java generics, at that point, we need to typecast the ArrayList each time we recover information from it. It is a major headache to typecast at each recovery.
If we utilize the Java generics in our code, then we need not do typecasting at each recovery. The below code shows this concept:
4. Implementing non-generic algorithms
We can perform the calculations that work on various sorts of items by utilizing generics in?Java , and they are type-safe as well.
Thanks to : techvidvan