What are Generics?
Introduced in Java 5, Generics allow you to create classes, interfaces, and methods that can work with different data types while providing compile-time type safety. Instead of using the generic Object type and performing manual casts, you can specify the type that a class or method will work with. This is indicated by the angle bracket notation <>.
The Problem Before Generics
Before generics, collections like ArrayList stored everything as an Object. This meant you could add any type of object to a collection, leading to potential ClassCastException errors at runtime.
Pre-Generics Code (Unsafe):
import java.util.ArrayList;
import java.util.List;
List list = new ArrayList();
list.add("hello");
list.add(123); // No error at compile time
// This will compile, but throw a ClassCastException at runtime
String text = (String) list.get(1);
To find this bug, you had to run the program. The compiler couldnโt help.
The Solution: Generics for Type Safety
Generics solve this problem by allowing you to specify the type of elements a collection can hold.
Code with Generics (Type-Safe):
import java.util.ArrayList;
import java.util.List;
List<String> list = new ArrayList<>(); // This list can ONLY hold Strings
list.add("hello");
// list.add(123); // This is now a COMPILE-TIME ERROR!
String text = list.get(0); // No cast needed
The compiler now enforces that only String objects can be added to the list. This catches bugs early and eliminates the need for manual casting.
Key Concepts of Generics
1. Generic Classes and Interfaces
You can create your own classes and interfaces that are generic. The type parameter (commonly T for Type, E for Element, K for Key, V for Value) acts as a placeholder.
Example of a Generic Class:
// A generic Box class that can hold any type of object
public class Box<T> {
private T content;
public void setContent(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
// Usage
Box<String> stringBox = new Box<>();
stringBox.setContent("A string");
String myString = stringBox.getContent();
Box<Integer> integerBox = new Box<>();
integerBox.setContent(42);
int myInt = integerBox.getContent();
2. Generic Methods
You can also create a generic method that has its own type parameter. This is useful for static utility methods.
public class Utils {
// A generic method to print array elements
public static <E> void printArray(E[] inputArray) {
for (E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
}
// Usage
Integer[] intArray = { 1, 2, 3 };
String[] stringArray = { "A", "B", "C" };
Utils.printArray(intArray); // Prints 1 2 3
Utils.printArray(stringArray); // Prints A B C
3. Bounded Type Parameters (Wildcards)
Sometimes you want to restrict the types that can be used as type arguments. This is done using the extends keyword.
- Upper Bounded Wildcard (
? extends Type): The unknown type is a subtype ofType. This is useful when you want to read from a generic structure.
// This method can take a List of Number or any of its subclasses (Integer, Double, etc.)
public void processNumbers(List<? extends Number> list) {
for (Number num : list) {
System.out.println(num.doubleValue());
}
// list.add(1); // COMPILE ERROR: You can't add to an upper-bounded list
}
- Lower Bounded Wildcard (
? super Type): The unknown type is a supertype ofType. This is useful when you want to write to a generic structure.
// This method can take a List of Integer or any of its superclasses (Number, Object)
public void addIntegers(List<? super Integer> list) {
list.add(10);
list.add(20);
// Object item = list.get(0); // You can only safely read as Object
}
The mnemonic PECS (Producer Extends, Consumer Super) helps remember when to use which wildcard.
Key Takeaway
Generics are a cornerstone of modern Java programming. They provide strong type checking at compile time, eliminate the need for explicit casts, and allow developers to write more reusable and robust code. Always use generics with collections and consider creating your own generic classes and methods to improve the quality of your code.
Leave a comment