0

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.

AhmadRaza
  • 5
  • 6
  • Sorry, I have a really hard time to understand your question. It would be better: include the OUTPUT of your code, and explicitly tell us what output is unexpected, and what you expect instead. See [mcve]. – GhostCat Feb 23 '22 at 13:37
  • Also note: why downcasting? You have a generic class, one time you instantiate for Integer, the other time for String. And as you print the CLASS of the value you stored, you get that: Integer.class resp String.class. – GhostCat Feb 23 '22 at 13:38
  • "*Because type erasure changes the return type to object.*" I think you're under the impression that type erasure basically makes the type of `T` unknowable by the compiler, so you're wondering how it knows to print `String` and `Integer`. Is that what you're asking? – Federico klez Culloca Feb 23 '22 at 13:43
  • yes i was asking that – AhmadRaza Feb 23 '22 at 13:47

1 Answers1

0

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614