Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
Chaturmind
← Java Core Fundamentals

Object-Oriented Programming

  • Classes and Objects
  • Inheritance and Polymorphism
  • Interfaces and Abstract Classes

Collections Framework

  • List, Set, and Map
  • Generics

Exceptions & Best Practices

  • Exception Handling
  • equals() and hashCode()
  • String Manipulation
HomeLearnJavaJava Core FundamentalsCollections Framework
✓ FreeIntermediate· 12 min read

Generics

Write type-safe, reusable code with Java generics.

Published January 24, 2025


Java Generics

Generics allow you to write classes and methods that work with any type while keeping compile-time type safety.

Generic Class

public class Box<T> {
    private T value;

    public Box(T value) { this.value = value; }
    public T get()      { return value; }
    public void set(T value) { this.value = value; }
}

Box<String>  stringBox = new Box<>("Hello");
Box<Integer> intBox    = new Box<>(42);

Generic Method

public static <T extends Comparable<T>> T max(T a, T b) {
    return a.compareTo(b) >= 0 ? a : b;
}

max(3, 7)       // → 7 (Integer)
max("apple", "banana") // → "banana" (String)

Bounded Type Parameters

// T must be a Number or subclass
public static <T extends Number> double sum(List<T> list) {
    return list.stream().mapToDouble(Number::doubleValue).sum();
}

Wildcards

// ? extends T — read-only (producer)
void printAll(List<? extends Number> list) {
    list.forEach(System.out::println);
}

// ? super T — write-only (consumer)
void addNumbers(List<? super Integer> list) {
    list.add(42);
}

PECS — Producer Extends, Consumer Super: use ? extends T when reading, ? super T when writing.

Type Erasure

At runtime, generic type information is erased. List<String> and List<Integer> are both just List at runtime. This is why you can't do new T[] or instanceof List<String>.

Interview Tip

Know PECS cold. Interviewers love asking: "Why can't you add to a List<? extends Number>?"

Because the compiler doesn't know the actual type at compile time — it could be List<Integer>, List<Double>, etc. Adding an Integer to a List<Double> would be a type error.

Previous

List, Set, and Map

Next

Exception Handling

AI Tutor

Lesson: Generics

Quick actions

AI responses can be inaccurate. Verify critical information.