I am stuck in generics downcasting. Because I learn type erasure. When code compiles all parameterized types are converted to the upper bound if the bound is not defined then it changes to object. Gen Class
public class Gen<T>
{
T ob;
Gen(T o)
{
ob = o;
}
T getob()
{
return ob;
}
void showType()
{
System.out.println("Type of T is: " + ob.getClass().getName());
}
}
GenDemo Class
public class GenDemo
{
public static void main(String [] args)
{
Gen<String> strob = new Gen<String>("I am Ahmad");
String str = strob.getob();
System.out.println("Value of str is: " + str);
}
}
String str = strob.getob(); is converted to String implictly. how JVM converted strob.getob() to String. From where JVM found the strob.getob() is downcast to string. Because type erasure changes the return type to object. So in byte code the return type of getob() is object.But when I call getob() it automatically downcast to string.So I am very confusing the downcasting in generic please explain me in detail.