NhacVibe
Ảnh bìa Java : Generics

Java : Generics

Oddly Yogi · 7 lượt xem · 13 thg 11, 2026

Mô tả

How can one piece of code store books today and members tomorrow, without losing type safety? In this video we learn Java generics. Like a storage locker with a label slot, a generic class is written once and labelled with a type each time it is used, and the compiler checks every item against the label. We see the problem generics solve with raw types and a real ClassCastException, then write a generic record, generic methods with type inference, bounded types with Comparable, and wildcards with ? extends and ? super (the PECS rule). We hit the limits of type erasure for real, and finish with a generic Repository interface and an InMemoryRepository class that Shelf uses for both books and members.

Versions: Java 25 LTS (Eclipse Temurin 25.0.5).

In this chapter:
00:00 A locker with a label slot
01:29 Life without generics
03:23 Our own generic type
04:28 Generic methods
05:31 Two type parameters
06:46 Bounded types
08:12 Why a list of Strings is not a list of Objects
08:52 Wildcards: extends for reading, super for writing
10:30 Type erasure and its limits
11:52 A generic repository
13:33 InMemoryRepository
14:51 Using the repository
16:27 What a wildcard will not allow
17:22 Several bounds at once
18:18 Generics all over the JDK
19:00 The compiler guards the bound
19:35 Wrap up

This is Chapter 7 of 15 in the "Java" series.
#Java #Java25 #Programming #LearnJava #JavaGenerics