100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Java Generics Cheat Sheet

Java Generics Cheat Sheet

Reference for Java generic classes, methods, bounded type parameters, wildcards, and type erasure for type-safe reusable code.

2 PagesIntermediateApr 15, 2026

Generic Classes

Defining a class with type parameters.

java
public class Box<T> {    private T value;    public void set(T value) { this.value = value; }    public T get() { return value; }}// UsageBox<String> box = new Box<>();box.set("hello");String s = box.get();

Bounded Type Parameters

Restrict type parameters to a class hierarchy.

java
// Upper bound: T must extend Numberpublic class NumberBox<T extends Number> {    private T value;    public double doubleValue() { return value.doubleValue(); }}// Multiple bounds (one class, multiple interfaces)public <T extends Comparable<T> & Cloneable> T max(T a, T b) {    return a.compareTo(b) >= 0 ? a : b;}

Wildcards

Use wildcards for flexible method parameters.

java
// Upper bounded wildcard: read-only, covariantpublic void printAll(List<? extends Number> list) {    for (Number n : list) System.out.println(n);}// Lower bounded wildcard: write-only, contravariantpublic void addIntegers(List<? super Integer> list) {    list.add(1);    list.add(2);}// Unbounded wildcardpublic void printSize(List<?> list) {    System.out.println(list.size());}

Generic Methods

Declare type parameters scoped to a single method.

java
public static <T> List<T> listOf(T... items) {    return Arrays.asList(items);}public static <K, V> void printEntry(Map.Entry<K, V> entry) {    System.out.println(entry.getKey() + "=" + entry.getValue());}// Explicit type witness when inference failsList<String> empty = Collections.<String>emptyList();

Key Concepts

Terminology every Java developer should know.

  • Type Erasure- Generic type info is removed at compile time; at runtime List<String> and List<Integer> share the same Class object.
  • PECS- "Producer Extends, Consumer Super" - use ? extends T when reading, ? super T when writing.
  • Raw Type- Using a generic class without type arguments (e.g. List list) bypasses type checking and is unchecked.
  • Bridge Method- Compiler-generated method that preserves polymorphism after type erasure in overriding generic methods.
  • Reifiable Type- A type whose full information is available at runtime (e.g. String, String[]); generic types are non-reifiable.
  • Unchecked Warning- Compiler warning issued when generic type safety can't be verified, e.g. casting to a generic type.
Pro Tip

You cannot create arrays of a generic type (new T[10] is illegal) because arrays are reified but generics are erased - use List<T> or an Object[] with an unchecked cast instead.

Was this cheat sheet helpful?

Explore Topics

#JavaGenerics#JavaGenericsCheatSheet#Programming#Intermediate#GenericClasses#BoundedTypeParameters#Wildcards#GenericMethods#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet