Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What is the concept of erasure in generics in java?

concept erasure generics Java
0
Posted

What is the concept of erasure in generics in java?

0

It’s basically the way that generics are implemented in Java via compiler trickery. The compiled generic code actually just uses java.lang.Object wherever you talk about T (or some other type parameter) – and there’s some metadata to tell the compiler that it really is a generic type. When you compile some code against a generic type or method, the compiler works out what you really mean (i.e. what the type argument for T is) and verifies at compile time that you’re doing the right thing, but the emitted code again just talks in terms of java.lang.Object – the compiler generates extra casts where necessary. At execution time, a List and a List are exactly the same; the extra type information has been erased by the compiler. Compare this with, say, C#, where the information is retained at execution time, allowing code to contain expressions such as typeof(T) which is the equivalent to T.class – except that the latter is invalid. (There are further differences between .NET

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123