Generics in Java allow classes, interfaces, and methods to work with different reference types using type parameters such as T, E, K, and V.
- Provide compile-time type safety by preventing incompatible type assignments.
- Improve code reusability and eliminate unnecessary explicit type casting.
import java.util.ArrayList;
class Geeks{
public static void main(String[] args){
ArrayList<String> names = new ArrayList<>();
names.add("Sweta");
names.add("Amit");
// names.add(10); // Compilation error
// No explicit type casting required
String name = names.get(0);
System.out.println(name);
}
}
Output
Sweta
Explanation: The ArrayList<String> accepts String objects. Adding an integer results in a compilation error, and retrieving a String does not require explicit type casting.
Types of Java Generics
Java supports generic classes, interfaces, and methods.
1. Generic Class
A generic class uses one or more type parameters that can be used to declare fields, method parameters, and return types.
class Box<T>{
T value;
Box(T value){
this.value = value;
}
T getValue(){
return value;
}
}
class Geeks{
public static void main(String[] args){
// Integer type
Box<Integer> b1 = new Box<>(15);
// String type
Box<String> b2 = new Box<>("GeeksForGeeks");
System.out.println(b1.getValue());
System.out.println(b2.getValue());
}
}
Output
15 GeeksForGeeks
Explanation: The type parameter T is used as a field type, constructor parameter type, and method return type. The same Box class works with both Integer and String objects.
Syntax:
class ClassName<T> {
T value;
}
2. Generic Interface
A generic interface declares type parameters that allow its implementing classes to work with different types.
interface Printable<T>{
void print(T value);
}
class Printer<T> implements Printable<T>{
public void print(T value) {
System.out.println(value);
}
}
class Geeks{
public static void main(String[] args){
Printer<String> p = new Printer<>();
p.print("GeeksForGeeks");
}
}
Output
GeeksForGeeks
Explanation: The Printable<T> interface declares a generic method signature using the interface's type parameter. The Printer<T> class implements the interface while retaining its generic type.
Syntax:
interface InterfaceName<T>{
void display(T value);
}
3. Generic Method
A generic method declares its own type parameters before its return type. It can be defined inside both generic and non-generic classes.
class Geeks{
// Generic method
static <T> void display(T value){
System.out.println(value);
}
public static void main(String[] args){
display(11);
display("GeeksForGeeks");
display(1.5);
}
}
Output
11 GeeksForGeeks 1.5
Explanation: The display() method accepts different reference types using the type parameter T. Primitive arguments are automatically converted into their corresponding wrapper objects through autoboxing.
Syntax:
static <T> void methodName(T value){
// Method body
}
Diamond Operator (<>)
The diamond operator was introduced in Java 7 to avoid repeating type arguments when creating generic objects. The compiler infers the type arguments from the context.
// Without diamond operator
Box<Integer> b1 = new Box<Integer>(10);
// With diamond operator
Box<Integer> b2 = new Box<>(10);
Both statements create a Box<Integer> object, but the second statement is shorter.
Limitations of Generics
- Generics do not support primitive types as type arguments. Use wrapper classes such as Integer and Double instead.
- Objects with incompatible generic type arguments cannot be assigned to each other.
- Generic arrays cannot be created directly using type parameters or non-reifiable parameterized types.
- The instanceof operator cannot verify generic type arguments that are unavailable at runtime. Use unbounded wildcards (<?>) where applicable.
- Static members cannot directly access the type parameters declared by their generic class.
Related article: Type Erasure in Java