Generics in Java

Generic is the most important in Java. In this article, we will read all detail of java generics.
Here is the table content of the article will we will cover this topic.
1. What is Java Generics?
2. Generic class
3. Generic method
4. Generic Interface
5. Advantages of Generics

What is Java Generics?

In Java, we can create a Generic class, Generic method, and Generic Interface that accepts the input to make it as a specific type. They can accept any type of data according to requirements.

Suppose you want to perform a task with three different data types of parameters.
You want to show data of String, int, and float type. You must create three functions that accept different inputs and perform the same task.
First of all, we will discuss it without a generic concept. Here we need to create three functions.

public class ExampleWithoutGeneric
{ 
	public static void main(String args[]) 
    { 
		ExampleWithoutGeneric obj = new ExampleWithoutGeneric();
		// Showing data of integer
		System.out.println("Calling by int values");
		obj.showIntData(5, 10);
		// Showing data of String
		System.out.println("Calling by String values");
		obj.showStringData("Hello", "Java");
		// Showing data of float
		System.out.println("Calling by float values");
		obj.showFloatData(1.23f, 3.1f);
    }
	
	public void showIntData(int a, int b)
	{
		System.out.println(a);
		System.out.println(b);
	}
	
	public void showStringData(String a, String b)
	{
		System.out.println(a);
		System.out.println(b);
	}
	
	public void showFloatData(float a, float b)
	{
		System.out.println(a);
		System.out.println(b);
	}
}

Output: Calling by int values
5
10
Calling by String values
Hello
Java
Calling by float values
1.23
3.1

Now we will see, how we can do it with Generics.

public class ExampleWithGeneric
{ 
	public static void main(String args[]) 
    { 
		ExampleWithGeneric obj = new ExampleWithGeneric();
		System.out.println("Calling by integer values");
		obj.showData(5, 10);
		System.out.println("Calling by String values");
		obj.showData("Hello", "Java");
		System.out.println("Calling by float values");
		obj.showData(5.1f, 10.2f);
		
    }
	
	public <T> void showData(T a, T b)
	{
		System.out.println(a);
		System.out.println(b);
	}
}

Output: Calling by integer values
5
10
Calling by String values
Hello
Java
Calling by float values
5.1
10.2

In this example, we can perform the task by the use of only a single method. It is a generic method that can accept any type of input. As of now do not focus on syntax we will discuss it in detail.

As you all familiar with the Collection framework. The Collection framework provides generic classes. Let’s take an example of the ArrayList class. ArrayList is a generic class because it can contain and return any type of data. Let’s see the syntax of ArrayList:

ArrayList<String> listOfString = new ArrayList<String>();
ArrayList<Integer> listOfInteger = new ArrayList<Integer>();

As you can see the ArrayList class can take any type of data.

Java generic type parameter

Here is the naming convention provided by Java that helps you to understand the Java Generic. The most used type parameter names are:
E – Element (Used in Collections Framework for example Set)
K – Key (Used in Collections Framework  for example Map)
N – Number
T – Type
V – Value (Used in Collections Framework  for Map)

Leave a Comment