coderz.py

Keep Coding Keep Cheering!

Generics in Java

java thread class

Generics are types that have parameters. The goal is to make it possible for methods, classes, and interfaces to take types (Integer, String, etc., and user-defined types) as a parameter. Generics can be used to build classes that interact with various data types. A generic entity is one that works on a parameterized type, such as a class, interface, or function.

Before generics, we could store any kind of non-generic object in the collection. The Java programmer is now required by generics to store a specific object type.

Syntax:
Class_Name <Type> obj = new Class_Name <Type>()

This is sometimes referred to as the Diamond Notation of constructing a Generic type object.

Example of Generic class:
// We use < > to specify Parameter type
class Coderz<C> 
{
	// An object of type T is declared
	C obj;
	Coderz(C obj) 
	{
	    this.obj = obj; 
	    
	} // constructor
	public C getObject() 
	{
	    return this.obj;
	    }
}

// Driver class to test above
class Main {
	public static void main(String[] args)
	{
		// instance of Integer type
		Coderz<Integer> iObj = new Coderz<Integer>(20);
		System.out.println(iObj.getObject());

		// instance of String type
		Coderz<String> sObj
			= new Coderz<String>("Coderzpy.com_JAVA");
		System.out.println(sObj.getObject());
	}
}
Output:
20
Coderzpy.com_JAVA
Generics Only Work with Reference Types:

A reference type must be given as the type argument when declaring an instance of a generic type. Primitive data types like int and char are not allowed.

Java Generics Type Parameters:

To fully understand generics, one must be familiar with the naming conventions for type parameters. The following are the typical type parameters:

  • Type (T)
  • element (E)
  • key (K)
  • number (N)
  • value (V)

Generic Types Differ Based on Their Type Arguments: Unless their type argument is the same, references to one generic type cannot be made to another generic type.

Generic Functions:

Depending on the type of arguments supplied to the generic method, we can also create generic functions that can be called with various sorts of arguments. Each method is handled by the compiler.

Type generic with several parameters
// Java program to show multiple
// type parameters in Java Generics

// We use < > to specify Parameter type
class Coderz<T, U>
{
	T obj1; // An object of type T
	U obj2; // An object of type U

	// constructor
	Coderz(T obj1, U obj2)
	{
		this.obj1 = obj1;
		this.obj2 = obj2;
	}

	// To print objects of T and U
	public void print()
	{
		System.out.println(obj1);
		System.out.println(obj2);
	}
}

// Driver class to test above
public class Main
{
	public static void main (String[] args)
	{
		Coderz <String, Integer> obj =
			new Coderz<String, Integer>("Coderzpy", 20);

		obj.print();
	}
}
Output:
Coderzpy
20

Note: also read about the Java Networking 

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

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

Advertisement