I canβt tell you exactly what you will get, but the one difference is that for method 2 you can specify the type of the parameter Class<String> , whereas for method 1 you just know there the parameter named T , but you donβt know the exact of type, except for the class in which T declared, would be subclassed by a concrete class for T
Example:
class Foo<T> { public void method1( T arg1 ) { ... } } class Bar extends Foo<Baz> { ... } Foo<?> foo = new Foo<Baz>(); Bar bar = new Bar();
In foo you cannot get type T at runtime (you don't know its Baz ) or at compile time. For bar you can get a type for T , since it is already known at compile time.
Another difference when viewing the code:
The call to getGenericParameterTypes() in method 1 should return a type T , the caller for method 2 should return Class<String> . However, if you call getTypeParameters() , you will get a type T for method 1, but a zero-length array for method 2.
Edit: since instead of getTypeParameters() instead of getTypeParameters() was specified the difference that I see from the code:
There was no difference for method 2, since if Generics are not used in the signature, getGenericParameterTypes() does indeed call getParameterTypes() . For method 1, getGenericParameterTypes() will return ParameterizedType , which indicates that the parameter is named T , while getParameterTypes() returns the required base type class, for example. Class<Object> for <T> or Class<Number> for <T extends Number> .
Thomas
source share