bounded type parameter
A type parameter restricted to types that support a capability, such as T extends Comparable<T>, so the code can call that capability.
In more detail
An upper bound written with extends lets a generic method call the bound's methods, such as compareTo, without a cast. The compiler rejects type arguments that do not meet the bound. A lower bound, written with super, appears in wildcards for collections you write into.
Example
public static <T extends Comparable<T>> T larger(T left, T right)Related terms
- Comparable — The standard interface a type implements to define its natural ordering, through a compareTo method.
- generic wildcard — The question mark in a generic type, as in a list of some unknown subtype of Number, meaning some specific type that this code does not know.
- PECS — A rule of thumb for generic wildcards: a collection that produces values for you uses extends, and one that consumes values from you uses super.
Taught in
- Generics Fundamentals — Modern Java: Virtual Threads & High-Throughput Services
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- Fifty Items, Five Traps — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Arrays: Declare, Sort, Search, Compare — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Final Review And Exam Day — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep