Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is meant by backwards compatibility in case of Java Generics?

Tags:

java

generics

Generic type information is not available at runtime in Java for backwards compatibility reasons.

What exactly compatibility reasons mean here? Also is generic type information available in some cases or is it ALWAYS unavailavble and are there any other reasons than backwards compatibility?

I have some knowledge about it but I would like to finally understand it if anyone would be kind enough to answer this question. I have read read several articles and bits of Oracle documentation but they weren't focused on the problem I'm trying to figure out here.

like image 299
Lucas Avatar asked Oct 23 '25 19:10

Lucas


1 Answers

Type information is always erased. Imagine you have a set of strings, you would (now) use Set<String> - but before generics were added it would be simply Set. To ensure backwards compatibility when generics were added to Java (in 2004 with J2SE 5.0) the type information is removed. So Set<String> compiles to Set to allow compatibility with code written before generics were added.

like image 142
frostmatthew Avatar answered Oct 26 '25 09:10

frostmatthew