Possible Duplicate:
C# vs Java generics
Java
use Type erasure while C#
keep type information at runtime, what are the practical difference in the behaviors of the language from this design?
Possible Duplicate:
C# vs Java generics
Java
use Type erasure while C#
keep type information at runtime, what are the practical difference in the behaviors of the language from this design?
There are a lot of issues with type erasure. It brings back bad memories. I haven't used Java since 1.6, so this may be out of date, but some things I remember:
class c implements IComparable<MyClass>, IComparable<MyOtherClass>
is impossible.
List<T1>
, List<T2>
, and so on. For example, Java -- How to deal with type erasure in constructors?Here's an example of something that's possible only if the type information is kept at runtime:
public string DoSomething<T>()
{
return typeof(T).Name;
}
The closest you could get in Java is something like:
public <T> string DoSomething(Class<T> theClass)
{
return theClass.getName();
}
Something that happens in Java due to type erasure is the creation of synthetic methods called "Bridge methods".
This usually happens when Java tries to compile a class or interface that extends a parameterized class, or implements a parameterized interface. In this case the Java compiler may need to create a synthetic method (bridge method) as part of the type-erasure process, which appears in the stack trace.
The compiler does this to preserve polymorphism of generic types after the type erasure process.
Java's documentation has an example.