Saturday, November 17, 2012

JVM type erasure

I think the best way to explain type erasure in the JVM (Java Virtual Machine) is to see what happens when you define to methods with different arguments.... oh wait not so different due to type erasure:


Try running this code (click on the clone icon to go to www.ideone.com, and once there press the "submit" button) and you'll see how the compiler gets all confused by you trying to define the same method twice....

In any case, this a documented restrictions, so spending a bit of quality time with Java's online tutorials, will certainly reduce the amount of surprise :)

http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#cannotOverload