generics-and-wildcards
Generics
public class GenericContainer<T> {
private T value;
public GenericContainer(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public static void main(String[] args) {
// Create a GenericContainer for an Integer
GenericContainer<Integer> intContainer = new GenericContainer<>(42);
int intValue = intContainer.getValue();
System.out.println("Integer Value: " + intValue);
// Create a GenericContainer for a String
GenericContainer<String> strContainer = new GenericContainer<>("Hello, Generics!");
String strValue = strContainer.getValue();
System.out.println("String Value: " + strValue);
}
}
Popular generic type parameter naming convention
Type Parameter
When to use?
Wildcard
Unbounded wildcard
Upper-bounded wildcard
Lower-bounded wildcard
Bounded wildcards illustration

When to use generic or wildcards?
Last updated