java - Get parameterized type class of a generic variable -
public class foo { public <t> void foo(final t t) { final class<t> clazz = (class<t>) t.getclass(); } }
i can not understand why t.getclass()
return class<?>
, not class<t>
(or class<? extends t>
) , why need make cast.
in way cast can fail ?
generics in java implemented using type erasure.
the type t not exist @ runtime , compiler substitutes t
java.lang.object
. following method compiled in bytecode:
public void foo(final object t)
Comments
Post a Comment