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.
Class_Name <Type> obj = new Class_Name <Type>()
This is sometimes referred to as the Diamond Notation of constructing a Generic type object.
// 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());
}
}
20
Coderzpy.com_JAVA
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.
To fully understand generics, one must be familiar with the naming conventions for type parameters. The following are the typical type parameters:
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.
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.
// 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();
}
}
Coderzpy
20
Note: also read about the Java Networking
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…