I read about Java's type erasure on Sun's website (http://java.sun.com/docs/books/tutorial/java/generics/erasure.html).
I have the following question :
When does type erasure occur - at compile time / runtime:when the class is loaded / runtime:when the class is instantiated.
A lot of sites (including the Sun tutorial mentioned above) say type erasure occurs at compile time. If the type information is completely removed at compile time, how does the JDK check type compatibility when a method using generics is invoked with no type information or wrong type information.
Consider the following example : say class A has a method empty(Box<T extends Number> b)
. We compile A.java and get the class file A.class. Now we create another class B which invokes the method empty with a non-parameterized argument - empty(new Box())
. If we compile B.java with A.class in the classpath, javac is smart enough to raise a warning. So A.class has some type information stored in it.
My guess would be that type erasure occurs when the class is loaded, but it is just a guess. Does anybody here know for sure?