java - Using generic interfaces -


this question has answer here:

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

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -