Preliminary conclusion: Generic type conversion is not reliable;
Source:
Package Com.srie.testjava; Public class Testclassdefine4<t> { publicstaticvoid main (string[] args) { TestClassDefine4 New Testclassdefine4<string>(); = T4.badcast (1); System.out.println (cast); } T Badcast (Integer i) { return// here is warning }}
Anti-compilation:
Package Com.srie.testjava; Import Java.io.PrintStream; Public class Testclassdefine4<t>{ publicstaticvoid main (string[] args) { new TestClassDefine4 (); = (String) t4.badcast (integer.valueof (1)); System.out.println (cast); } T Badcast (Integer i) { return i; }}
Run Exception:
Exception in thread "main" Java.lang.ClassCastException:java.lang.Integer cannot is cast to java.lang.String at Com.srie.testjava.TestClassDefine4.main (Testclassdefine4.java:7)
Type conversion problems caused by Java generic type erasure