Home > database >  DownCasting in generics in java
DownCasting in generics in java

Time:02-24

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.

CodePudding user response:

Yes, at runtime the return type of strob.getob(); is basically equivalent to just Object.

However, during compilation the compiler understands that strob is a Gen<String> and ensures that all the necessary low-level operations are added. So it basically treats that line as if you had written

String str = (String) strob.getob();

In other words: it will generate the bytecode for the type cast, even though that typecast is nowhere to be found in the source code.

This also explains when for some reason the generic type system is broken (by using unchecked casts, basically): if for some reason getob() actually returns something that can't be cast to String then you'll get a boring old ClassCastException just as if you had tried to cast an Integer to String.

  • Related