Why compiler not stopped to write such code and throw runtime error.
public class GenericTest<T,S> {
T x;
S y;
public GenericTest(T x,S y) {
this.x=x;
this.y=y;
}
void display(){
System.out.println("Display->"+((int)x)+y); //why does it throw error runtime instead of compile time?
}
}
When calling this will obviously failed.
public class Test {
public static void main(String[] args) {
GenericTest<String, String> s=new GenericTest<String, String>("Hello","World");
s.display();
}
}
why does it allowed to type cast for generic type:
System.out.println("Display->"+((int)x)+y);