Why this happened? One line in the code works well while the other similar line doesn't. Does the automatic type cast only happen in certain conditions? I have tried to assign gt.echoV() to an Object and it works well; but when I assign it to a String, the same error will come out again.
public class GeneMethodTest {
public static void main(String... args) {
GeneMethodTest gt = new GeneMethodTest();
gt.<String>echoV(); //this line works well
gt.<String>echoV().getClass();//this line leads to a type cast exception
}
public <T> T echoV() {
T t=(T)(new Object());
return t;
}
}