Hi Manfred, This was not clear for me. Why does the compiler behaves like this?? it is during the runtime in which the generic syntax has no role to play. I am bit confused... can you please elaborate on the same???
It is called type erasure. Compiler removes all the type information from the code before compilation is done. The type information that you study under generics is only to restrict the programmer to not be able to do unsafe operations.
For example, the parameterized type List<String> is translated to type List, which is called raw type. The same happens for the parameterized type List<Double>; it also appears as List in the byte code.
After translation by type erasure, all information regarding type parameters and type arguments has disappeared. As a result, all instantiations of the same generic type share the same runtime type, namely the raw type.
Example (printing the runtime type of two parameterized types):
prints: runtime type of ArrayList<String>: class java.util.ArrayList runtime type of ArrayList<Double> : class java.util.ArrayList
Hi Chandra, I think in example #1 given by you,it wont give compiler error rather it will go to function which is taking string arg. the same is explained in the link which you provided above for discussion link.