What is the concept of erasure in generics in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Erasure, literally means that the type information which is present in the source code is erased from the compiled bytecode. Let us understand this with some code.
If you compile this code and then decompile it with a Java decompiler, you will get something like this. Notice that the decompiled code contains no trace of the type information present in the original source code.
To summarize the previous answers:
In general, here is how erasure works. When your Java code is compiled, all generic type information is removed (erased). This means replacing type parameters with their bound type, which is
**Object**
if no explicit bound is specified, and then applying the appropriate casts(as determined by the type arguments) to maintain type compatibility with the types specified by the type arguments. The compiler also enforces this type compatibility.This approach to generics means that no type parameters exist at run time. They are simply a source-code mechanism.