Given the following piece of code:
public class ClassCastTest {
private static class GenericHolder<T> {
private T object;
public GenericHolder(Object object) {
this.object = (T) object;
System.out.println(getObject());
}
public T getObject() {
return object;
}
}
public static void main(String[] args) {
GenericHolder<String> foo = new GenericHolder<>(3l);
System.out.println(foo.getObject());
}
}
Why does Java throw a ClassCastException in the second line of the main-method instead of the second line of the GenericHolder?
Because of the way generics are implemented in the language, your cast to (T) doesn't actually do anything. It's only when you use a generic type in a way that actually gets out a concrete type -- here, System.out.println expects a String and it does the cast to get it -- that the runtime actually does any casting.
As far as the Java runtime is concerned, there's no difference between a GenericHolder<String> and a GenericHolder<Integer>; they both hold an Object. Java just inserts casts anywhere you get a concrete type out of a generic type.
Research type erasure for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With