mercredi 11 mars 2015

'U extends Something' VS just 'Something' in Bounded Type Parameters



Suppose I have a bounded type parameter in a generic method (an example from the The Java™ Tutorials, http://ift.tt/1qZyTjz):



public static <U extends Number> void inspect(U u) {
}


Then, I can call using any Number subtype arguments:



inspect(1);
inspect(1.0);
inspect(1.0f);


However, this is just the same as having a method with a Number parameter:



public static void inspect2(Number u) {
}

inspect2(1);
inspect2(1.0);
inspect2(1.0f);


What would be the benefits using Bounded Type Parameters (extends) in generic methods?


Note that not like



List<Map<String, String>> vs List<? extends Map<String, String>>


these generic methods do not require/need any subtype relationships.




Aucun commentaire:

Enregistrer un commentaire