Erase Generics

I would like to know why it would be a problem if Java had generics without erasure. I know that the problem is compatibility with older libraries, but is it worth it to put Object instead of the type where we will not specify it. For example, we can use List list = new ArrayList(); as List<Object> -s collection, and List<Integer> list = new ArrayList<Integer>(); will be the same as without erasure.

Can someone please show an example of what happens if erasure does not come into play.

+4
source share
3 answers

It would still require that these old libraries be recompiled before they could be used on the new JVM - a change that Java developers have always avoided at all costs.

+3
source

One of the best resources that you can find for understanding Java generics in general and erasing styles in particular is the Angelika Langer FAQ on the topic, take a look at it and everything will be clear.

+3
source

This was not just a backward compatibility issue, but migration compatibility . As you pointed out correctly, pre-1.5 each list was actually List<Object> , so the definition of List := List<Object> would work and be backward compatible. However, porting existing libraries by 1.5 and turning the implicit List<Object> into List<Foo> will break the client code. Enter the source types.

0
source

Source: https://habr.com/ru/post/1383625/


All Articles