GENERICS IN JAVA

What are generics in java?

Generics is a term that denotes a set of language features related to the definition and use of generics types and methods.

Java generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code stable by detecting the bugs at compile time.

Java generic methods differ from regular data types and methods so before generics we used collections to store any type of object it can be either generics or non-generic objects now generics force the java programmer to store a specific type of object.

Why java generics?

When the data type is not fixed it is integer or float then checked the data type and assigned the current data type.

Code that uses generics has many benefits over non-generic code:

  • Stronger type checks at compile time.
    A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.

Elimination of casts.
The following code snippet without generics requires casting:

When re-written to use generics, the code does not require casting:

  • Enabling programmers to implement generic algorithms.
    By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type-safe and easier to read.

Type Parameters

The type parameters naming conventions are important to learn generics thoroughly. The common type parameters are as follows:

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

Type of java generics:-

  1. Generic type class
  2. Generic Method
  3. Generic Interface
  4. Generic Constructor
  1. Generics type class:-

A class is said generics if it declares one or more types of a variable so this variable is known as a parameter of the java class.

Let’s see a simple example to create and use the generic class.

Ex:-

Let’s another example:-

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.

  1. Generic Method:-

We can write a single generic method declaration that can be called with arguments of different types.

EX:-

 

IMPORTANT POINT:-

All generic method declarations have a type parameter section delimited by angle brackets (<>) that precedence the method’s return type.

  • Each type of parameter section contains one or more type parameters separated by commas.
  • The type parameters can be used to declare the return type.
  • Type parameter can represent only reference type not primitive type (like int, double, and char)
  1. Generic Interface:-

 

The MyInterface is a generic interface that declares the method called myMethod( ). In general, a generic interface is declared in the same way as a generic class. In this case, the type parameter is T. Next, MyClass implements MyInterface. Myclass is a non-generic class. Of course, if a class implements a specific type of generic interface, then the implementing class does not need to be generic.

We can create generic interfaces in java. We can also have multiple types of parameters as in the map interface. Again we can provide parameterized value to a parameterized type also.

 For Example:-

New HashMap<String, List<String >>(); is valid.

  1. Generic Constructor:-

Generic constructors are the same as generic methods. For generic constructors after the public keyword and before the class name the type parameter must be placed.  Constructors can be invoked with any type of parameter after defining a generic constructor. A constructor is a block of code that initializes the newly created object. It is an instance method with no return type. The name of the constructor is the same as the class name. Constructors can be Generic, despite their class is not Generic.

Example:-// Java Program to illustrate Generic constructors

Output:-

Number to Double Conversion:

v: 10.0

v: 136.8000030517578

Leave a Reply