Grokking Java Interview Questions
0% completed
Bounded Types
static <T> T larger(T a, T b) { return a.compareTo(b) >= 0 ? a : b; }
This method does not compile. The message says cannot find symbol, and it names method compareTo(T) as the missing symbol. The compiler has no information about T, and the only methods it can rely on are those of Object, which has no compareTo.
A type parameter can be declared with more information than this. The four answers show what a bound permits, how a bound works together with inheritance, how one parameter can have several bounds, and how a class can appear inside its own bound.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%