Java generics

Java Generics

Java generic methods help the user to specify a set of related methods using a single method declaration. The user can also specify a set of relatable objects using a single class declaration. Generics in java help in ensuring compile-time safety which helps the programmers to catch invalid arguments during the time of compilation.

Java Generic methods and classes

  • All the generic methods include a type parameter section with < > brackets which precede the type of method.
  • Each type parameter section has another section that contains one or more type parameters. We separate these parameters by commas. It will specify a generic type name in the program.
  • We use the parameters for the return type. They behave like placeholders for all the arguments passed in the generic methods. These arguments are also known as actual type arguments.
  • There’s no difference between declarations of generic method’s body. However, the generic type can only represent the reference type and not the primitives such as double, char etc

What are the advantages of generics in Java

Generic class in java enables the use of classes and interfaces to themself turn into parameters. The user performs it while creating other classes or interfaces. The user can reuse the same code multiple times with different inputs with the help of generics.

The class helps to hold a single type of object as it cannot store multiple object types. There is no need for type casting while using generics. Otherwise, we always needed to typecast when not using generics. A good programming strategy suggests the problems should be handled during the compile-time only. So in generics, the issues are checked at the time of compilations, hence there is no scope of any error during the run time.

There’s the use of (?) symbol in generics which means a wildcard element. Wildcard element promotes any type. It is not allowed to use a wildcard as a type argument for a generic method but we can use it as a type of parameter, a field or return type.

Generic class Syntax in java

class Gen<G>{  
G obj;  
void add(G obj){this.obj=obj;}  
G get(){
return obj;}  

In this syntax, the type G in generic class represents that it can refer to any string, int, or employee type. We use the specified type to store and retrieve data.

The classes ate denoted in the following manner:

T – Type

E – Element

K – Key

N – Number

V – Value

Generic class in Java with example

public class Developerhelps{
public static < E > void printArray( E[] inputArray ) {
for(E element : inputArray) {
System.out.print(element+" ");
public static void main(String args[]) {
Integer[] intArray = { 10, 20, 30, 40, 50 };
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
Character[] charArray = { 'M', 'E', 'G', 'H', 'A' };
System.out.println("IntArray contains:");
System.out.println("doubleArray contains:");
System.out.println("charArray contains:");

The output of the above java code will be:

intArray contains:
10 20 30 40 50 

doubleArray contains:
1.1 2.2 3.3 4.4 

charArray contains:

Leave a comment

Your email address will not be published. Required fields are marked *