Java Generics

Java Generics

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.

No alt text provided for this image


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

No alt text provided for this image

Output:

18

Code to understand Generic Classes:

No alt text provided for this image

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:

No alt text provided for this image


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:

  1. T – Type
  2. E – Element
  3. K – Key
  4. N – Number
  5. V – Value

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

  • There should be a type parameter section in all generic method declarations, delimited by angular brackets?<>?that precede the method’s return type.
  • If there is more than one parameter in the parameter list then each type parameter should be separated by commas.
  • We can also use the type parameters to declare the return type and let them act as placeholders for the types of arguments passed to the generic method, called as actual type arguments.
  • The method body of a generic method is declared similar to any other non-generic method.
  • The type parameter in a method can represent only reference types, non-primitive types like int, double and char.

Code to understand Generic Methods:

No alt text provided for this image

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.

No alt text provided for this image

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,

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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.

No alt text provided for this image

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:

No alt text provided for this image

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





要查看或添加评论,请登录

Omar Ismail的更多文章

  • OAuth Grant Types (Authorization Code Grant)

    OAuth Grant Types (Authorization Code Grant)

    The authorization code grant type is used to obtain both access tokens and refresh tokens. The grant type uses the…

  • What is Apache Kafka?

    What is Apache Kafka?

    Reference : https://www.qlik.

    2 条评论
  • Multi-Tenant Architecture in a Nutshell

    Multi-Tenant Architecture in a Nutshell

    Thanks to the original writer and article :…

  • Microservices Communication!

    Microservices Communication!

    Thanks To: https://medium.com/design-microservices-architecture-with-patterns/microservices-communications-f319f8d76b71…

    2 条评论
  • What Are the New Features of SpringBoot3 ?

    What Are the New Features of SpringBoot3 ?

    Thanks to : https://medium.com/javarevisited/what-are-the-new-features-of-springboot3-6ddba9af664 1.

    1 条评论
  • OAuth 2.0!

    OAuth 2.0!

    Thanks to the original writer : https://medium.com/@isharaaruna OAuth2.

    2 条评论
  • How to Draw a Technical Architecture Diagram

    How to Draw a Technical Architecture Diagram

    Thanks to the original writer and article : https://levelup.gitconnected.

    2 条评论
  • Event Sourcing Versus Event-Driven Architecture

    Event Sourcing Versus Event-Driven Architecture

    Thanks to the original writer and article :…

  • Best Practices For Your API Versioning Strategy

    Best Practices For Your API Versioning Strategy

    API versioning is critical. But do you know all of the API versioning best practices? Is your API versioning strategy…

    1 条评论
  • Enterprise Architecture Tools

    Enterprise Architecture Tools

    Thanks to the original writer and article : https://medium.com/geekculture/enterprise-architecture-tools-b8165c8c9d7…

社区洞察