I've encountered a behaviour of generics in Java that I completely can't understand (with my .NET background).
public class TestGeneric
{
public void get (Object arg)
{
T temp = (T) arg;
System.out.println(temp.toString());
return;
}
}
TestGeneric tg = new TestGeneric();
tg.get("Crack!!!");
Please tell me why I'm not getting ClassCastException in get, moreover, in Idea I see temp as String after assignment and having value of "Crack!!!". Also, how could I have that ClassCastException throwed? I'm using JDK 1.7.0_07 on Windows 7 x64.
解决方案
The reason you are not getting a class cast exception is that Java generics are implemented through type erasure. Unlike .NET generics that required significant changes to CLS, Java generics are processed entirely in compile-time. At runtime, the cast to T is ignored. In order to check type at runtime, you need to store Class, and use its methods to check the type of a parameter passed in:
public class TestGeneric
{
private Class genClass;
public TestGeneric(Class t) {genClass = t;}
public void get (Object arg)
{
if (!genClass.isInstance(arg)) {
throw new ClassCastException();
}
T temp = (T) arg;
System.out.println(temp.toString());
return;
}
}
TestGeneric tg = new TestGeneric(Integer.class);
tg.get("Bang!!!"); // Now that's a real Bang!!!