018GenericsinJava
018GenericsinJava
Generics in Java
Parameterizedtypes mean generic. Generics allow types (Character, Integer,
String, …, etc., as well as user-defined types) to act as parameters to interfaces,
classes, and methods. Generics in Java facilitate the creation of classes that
deal with different types of datatypes. Entities, such as methods, interfaces,
classes that act on the parameterized type,are known as the generic entity.
Generics is similar to template C++ templates.
Need of Generics in Java
We know that theObject class is the parent class of all the Java classes, and
hence,it is obvious that the Object reference can refer to any object. These
features do not ensure type safety.To achieve the same in Java, we
use Generics.
Generics Class in Java
Similar to C++, we use angular brackets (<>) to mention parameter types in the
creation of the generic class.
Syntax:
// For creating an object of a generic class
// Generic class
class ABC<Type>
Type obj;
ABC(Type obj)
{
this.obj = obj;
return this.obj;
// main method
}
Output:
The value of the integer is: 17
// A reference of type U
U uObj;
// A reference of type T
T tObj;
this.uObj = obj1;
this.tObj = obj2;
}
// For printing the objects of types T and U
System.out.println(uObj);
System.out.println(tObj);
// Driver class
// main method
}
Output:
The value of the integer is: 10
// Generic functions
// main mehtod
obj.display(151);
obj.display(1.51f);
obj.display(1.67);
}
Output:
java.lang.Integer = 151
java.lang.Float = 1.51
java.lang.Double = 1.67
// Generic class
class ABC<P>
P obj;
// main method
}
Output:
Explanation:Even thoughintObj and strObj are the objects of the same class
ABC, they represent the difference in parameter type. Hence, the error occurs,
which shows that Generics ensures type safety.
Not only this, but Generics also helps to make errors appear at the compile time.
Supposethere is an ArrayList (without Generic) that stores the person’s name,
and if someone stores a number by mistake, this mistakeignored by the compiler.
Observe the following code.
FileName: GenericsExample4.java
// A Simple Java program to demonstrate that NOT using
// importing ArrayList
import java.util.ArrayList;
// main method
personNames.add("Rahul Dravid");
personNames.add("Virender Sehwag");
personNames.add("Sachin Tendulkar");
personNames.add("Zaheer Khan");
personNames.add("Virat Kohli");
personNames.add("Anil Kumble");
personNames.add(12); // Compiler accepts it without any
issue
System.out.println(personNames.get(i));
}
Output:
Rahul Dravid
Virender Sehwag
Sachin Tendulkar
Zaheer Khan
Virat Kohli
Anil Kumble
12
Explanation:Till this point, everything seems fine.However, it should not be fine,
as 12 should not be entertained in the person’s name list. The above code is
potent to throw an exception or error. Supposethe next task is to find the length
of strings present in the list, then at that time, we will face issues. Observe the
following code.
FileName: GenericsExample5.java
// A Simple Java program to demonstrate that NOT using
// importing ArrayList
import java.util.ArrayList;
public class GenericsExample5
// main method
personNames.add("Rahul Dravid");
personNames.add("Virender Sehwag");
personNames.add("Sachin Tendulkar");
personNames.add("Zaheer Khan");
personNames.add("Virat Kohli");
personNames.add("Anil Kumble");
System.out.println(personNames.get(i));
{
len[i] = ((String)personNames.get(i)).length();
}
Output:
// importing ArrayList
import java.util.ArrayList;
// main method
personNames.add("Rahul Dravid");
personNames.add("Virender Sehwag");
personNames.add("Sachin Tendulkar");
personNames.add("Zaheer Khan");
personNames.add("Virat Kohli");
personNames.add("Anil Kumble");
System.out.println(personNames.get(i));
len[i] = ((String)personNames.get(i)).length();
}
Output:
// importing ArrayList
import java.util.ArrayList;
// main method
personNames.add("Rahul Dravid");
personNames.add("Virender Sehwag");
personNames.add("Sachin Tendulkar");
personNames.add("Zaheer Khan");
personNames.add("Virat Kohli");
personNames.add("Anil Kumble");
System.out.println(name);
}
}
}
Output:
Rahul Dravid
Virender Sehwag
Sachin Tendulkar
Zaheer Khan
Virat Kohli
Anil Kumble
FileName: GenericsExample8.java
// A Simple Java program to demonstrate that type casting
// importing ArrayList
import java.util.ArrayList;
// main method
personNames.add("Rahul Dravid");
personNames.add("Virender Sehwag");
personNames.add("Sachin Tendulkar");
personNames.add("Zaheer Khan");
personNames.add("Virat Kohli");
personNames.add("Anil Kumble");
// size of the array list
System.out.println(name);
}
Output:
Rahul Dravid
Virender Sehwag
Sachin Tendulkar
Zaheer Khan
Virat Kohli
Anil Kumble
3) Reusability of Code:By creating a generic class or method, one can use
different parameter types on the same generic method. Thus, writing code
explicitly for different types is not required. The topmost example of in this
section shows the same.