Understanding Java Generics: Classes, Methods, Type Bounds, and Erasure
The article introduces Java generics—showing how they prevent runtime ClassCastExceptions by enforcing compile-time type safety, demonstrating generic class and method definitions, type bounds (including multiple bounds), erasure effects observable via reflection, and the use of wildcards for flexible type constraints.
From Java 1.5 the official generic mechanism was introduced, but generic concepts existed earlier. This article introduces the use of generics and why they improve type safety.
Example of a raw List causing ClassCastException :
public class Main {
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
dealList(list);
}
public static void dealList(List list) {
int result = 0;
for (Object obj : list) {
int num = (int) obj;
result += num;
}
System.out.println(result);
}
}Adding a String element leads to a runtime ClassCastException :
public class Main {
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add("4");
dealList(list);
}
}Using generics on collections prevents such errors at compile time.
Define a generic class:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Genericity
{
private T t1;
private T t2;
}Instantiate with Integer :
public static void main(String[] args) {
Genericity
genericity = new Genericity<>(1, 1);
Integer t1 = genericity.getT1();
Integer t2 = genericity.getT2();
int result = t1 + t2;
System.out.println(result);
}Generic class with two type parameters:
public class Genericity
{
private T t1;
private U t2;
}Instantiate with Integer and String :
public static void main(String[] args) {
Genericity
genericity = new Genericity<>(1, "1");
Integer t1 = genericity.getT1();
String t2 = genericity.getT2();
}Generic method example:
public static
void method(T t) { }Calling with explicit type:
Genericity.
method(1);Type bound to Comparable :
public static
T min(T[] t) {
T minimum = t[0];
for (int i = 0; i < t.length; i++) {
if (minimum.compareTo(t[i]) > 0) {
minimum = t[i];
}
}
return minimum;
}Multiple bounds example:
public static
T min(T[] t) { /* ... */ }Generic type erasure demonstration using reflection:
public static void main(String[] args) throws Exception {
List
list = new ArrayList<>();
list.add(1);
Method addMethod = list.getClass().getDeclaredMethod("add", Object.class);
addMethod.invoke(list, "2");
addMethod.invoke(list, true);
addMethod.invoke(list, 3.2f);
System.out.println(list);
}The result shows a heterogeneous list at runtime, confirming that generic checks are erased.
Wildcard usage examples:
Genericity
g1;
Genericity
g2;
Genericity
g3;Java Tech Enthusiast
Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.