#java #generics #genericsinjava #javagenerics
@kingoftechniques6864
The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is called a generic entity.
Generics are features of the Java programming language that allow programmers to write parameterized code. They were originally created to address some problems with collections. Thus the term generics-collections is usually used together today.
To understand generics, let’s see how it solves issues with collections.
This kind of problem would produce unsafe code with potential bugs as the type of the objects added to the collection cannot be verified by the compiler and compile time.
Here, we are using the T type parameter to create the generic class of specific type. Let's see a simple example to create and use the generic class. The T type indicates that it can refer to any type (like String, Integer, and Employee). The type you specify for the class will be used to store and retrieve the data.