I had read about type erasure in Java a long time back. However, today when a friend asked me a question related to type erasure, I found myself not quite certain of the answer.
I read up on it again and here is what I learned.
Angelika Langer has an excellent FAQ, where she explains generics, and type erasure. According to the FAQ:
A process that maps a parameterized type (or method) to its unique byte code representation by eliding type parameters and arguments.
OK, let's understand what that means. Below is a simple class which uses the generified version of Java Lists.
I am going to take the bytecode generated by the above class and decompile it using JAD.
Here is the decompiled Java class:
As you can see, the decompiled code has absolutely no trace of any generics information. This is because of erasure. When the Java compiler compiles client code which uses a library that has been generified, the client code is compiled in such a way that all generics information is erased from the bytecode.
This also explains the fact that generics information in client code is meant purely for the compiler to do type checking. Once the compiler has done it's job, this information is discarded.
The reason why Java uses type erasure is to maintain backwards compatibility. Let's assume you wrote some code a few years back which used Java Lists, before generics was introduced in the language. Obviously your code had no mention of generics. Now, when engineers at Sun decided to introduce generics, they did not want to break code which people had already written. One possible way was to ensure that client code which uses generics in the classes they invoke, never carries any information about generics in the compiled code. So the above class when compiled should not carry any information about generics. Because compiled client code never carries information about generics anyways, library implementers are free to add generics to their code without the worry of breaking anyone's old code.
Bruce Eckel has written a rather lenghty but good article on generics and erasure.
I read up on it again and here is what I learned.
Angelika Langer has an excellent FAQ, where she explains generics, and type erasure. According to the FAQ:
A process that maps a parameterized type (or method) to its unique byte code representation by eliding type parameters and arguments.
OK, let's understand what that means. Below is a simple class which uses the generified version of Java Lists.
|
I am going to take the bytecode generated by the above class and decompile it using JAD.
Here is the decompiled Java class:
|
As you can see, the decompiled code has absolutely no trace of any generics information. This is because of erasure. When the Java compiler compiles client code which uses a library that has been generified, the client code is compiled in such a way that all generics information is erased from the bytecode.
This also explains the fact that generics information in client code is meant purely for the compiler to do type checking. Once the compiler has done it's job, this information is discarded.
The reason why Java uses type erasure is to maintain backwards compatibility. Let's assume you wrote some code a few years back which used Java Lists, before generics was introduced in the language. Obviously your code had no mention of generics. Now, when engineers at Sun decided to introduce generics, they did not want to break code which people had already written. One possible way was to ensure that client code which uses generics in the classes they invoke, never carries any information about generics in the compiled code. So the above class when compiled should not carry any information about generics. Because compiled client code never carries information about generics anyways, library implementers are free to add generics to their code without the worry of breaking anyone's old code.
Bruce Eckel has written a rather lenghty but good article on generics and erasure.
Comments