0Pricing
Java Academy · Lesson

Generics Basics (T, inference)

Learn generic type parameters T , why they give compile-time safety, and how the diamond operator infers types.

Generics Basics (T, inference) is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why generics?

Generics add types to containers and utilities so the compiler can protect you. Benefits:

  • Type safety — no accidental mix of types.
  • No casts — cleaner code.
  • Reusable — one class works for many types.

What is T?

T (type parameter) is a placeholder for a real type. When you write Box<String>, the compiler treats T as String inside that usage.

Code: Box<T>

Box<T>: T yerine String verdiğimizde sadece String kabul edilir. Yanlış tür derleme zamanı hatasıdır; çalışma zamanı sürprizi yok.

public class Main {
  // A tiny generic container
  static class Box<T> {
    private T value;
    void set(T v) { value = v; }
    T get() { return value; }
  }

  public static void main(String[] args) {
    Box<String> bs = new Box<String>(); // T becomes String
    bs.set("hello");
    System.out.println(bs.get());       // no cast needed

    // Box<Integer> bi = new Box<Integer>();
    // bi.set("oops"); // would not compile: String is not Integer
  }
}

Code: diamond inference

<> (diamond) ile derleyici sağ taraftaki türü çıkarır. Soldaki türle uyumsuzsa derlemez; bu da güvenlik sağlar.

public class Main {
  static class Box<T> {
    private T value;
    Box() {}
    Box(T v) { value = v; }
    T get() { return value; }
  }

  public static void main(String[] args) {
    // Type on the left, diamond on the right — compiler infers T
    Box<Integer> a = new Box<>(123);
    Box<String> b = new Box<>("hi");

    System.out.println(a.get() + 1);    // 124
    System.out.println(b.get().toUpperCase());
  }
}

Bounded type parameters

Bounds: Bazı generic metotlar Comparable gibi bir yetenek ister. Örn: <T extends Comparable<T>> demek T karşılaştırılabilir olmalı.

Code: bounded generic method

Generic metot: <T extends Comparable<T>>. Integer ve String uyumludur, çünkü ikisi de Comparable uygular.

public class Main {
  // Generic method: works for any Comparable type
  static <T extends Comparable<T>> T min(T a, T b) {
    return (a.compareTo(b) <= 0) ? a : b;
  }

  public static void main(String[] args) {
    System.out.println(min(3, 7));              // Integer
    System.out.println(min("apple", "banana")); // String (lexicographic)
  }
}

Generics basics check

Quick check: What is the main benefit of using generics like Box<String>?

Recap

Recap: T bir yer tutucudur; diamond ile türü çıkarılır. Sınırlandırmalar (extends Comparable) ile generic metotları güvenli ve güçlü hâle getiririz.

Frequently asked questions

Is the “Generics Basics (T, inference)” lesson free?

Yes — the full text of “Generics Basics (T, inference)” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generics Basics (T, inference)”?

Learn generic type parameters T , why they give compile-time safety, and how the diamond operator infers types. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Generics Basics (T, inference)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Interfaces in Practice
  2. Generics Basics (T, inference)
  3. Generic Methods/Classes
← Back to Java Academy