java - Using generic interfaces -
this question has answer here:
- what raw type , why shouldn't use it? 12 answers
is there differences between 2 class declarations
1:
class myclass <t extends number & comparable> 2:
class myclass <t extends number & comparable<t>> i think there differences. cannot find example show differences because don't understand exact.
can show me example?
there difference. first 1 using raw types, , thus, less type-safe. example:
this works, should not work
class myclass<t extends number & comparable> { void use(t t) { string s = null; t.compareto(s); // works, cause runtime error } } whereas not work (because should not work)
class myclass<t extends number & comparable<t>> { void use(t t) { string s = null; t.compareto(s); // compile-time error } } edit: full code, requested:
class myclass<t extends number & comparable> { void use(t t) { string s = "laziness"; t.compareto(s); // works, cause runtime error } } public class myclasstest { public static void main(string[] args) { myclass<integer> m = new myclass<integer>(); integer integer = new integer(42); m.use(integer); } }
Comments
Post a Comment