Generic Method maximum and Specifying a Type Parameter’s Upper Bound

Generic method maximum (lines 17–28) uses type parameter T as the return type of the method (line 17), as the type of method parameters x, y and z (line 17), and as the type of local variable max (line 19). The type-parameter section specifies that T extends Comparable<T>—only objects of classes that implement interface Comparable<T> can be used with this method. Comparable<T> is known as the type parameter’s upper bound. By default, Object is the upper bound, meaning that an object of any type can be used. Type-parameter declarations that bound the parameter always use keyword extends regardless of whether the type parameter extends a class or implements an interface. The ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.