Java generic method type casting -
why happened? 1 line in code works while other similar line doesn't. automatic type cast happen in conditions? have tried assign gt.echov() object , works well; when assign string, same error come out again.
public class genemethodtest { public static void main(string... args) { genemethodtest gt = new genemethodtest(); gt.<string>echov(); //this line works gt.<string>echov().getclass();//this line leads type cast exception } public <t> t echov() { t t=(t)(new object()); return t; } }
gt.<string>echov().getclass();
produces equivalent of following sequence of operations:
// inside echov object t = new object(); // note not string! object returnvalue = t; // in main string stacktemp = (string) returnvalue; // operation fails stacktemp.getclass();
what "for free" generics (string)
cast. nothing else.
Comments
Post a Comment