Are inferred types reflected in byte code? [closed]

I am aware of type inference and type erasure in Java, but I am unsure about how they work. Say List<Integer> list = List.of(1) for instance. Since of(E... elements) takes in a variable number of some type of elements and returns List<E>, I suppose that E is inferred to be Integer.

The part that I am confused about is that since E is already inferred to be Integer, does E still erases to Object after type erasure at compile-time? How does Type erasure work and what is the significance of type inference if the inferred types are not “stored”/”carried forward”?


Yes, when compile, generics create only one specialization, using Object, let’s say that you have two list List<? extends Object> list = List.of(1) and List<? extends Object> strs = List.of("test"), in compile time both will be a List<Object>, for second question, type erasure is good for reducing code footprint, with only one specialization, and it reduces the memory cost of carry this information, important too talk about don’t allow reflection abuses, if we carry these information it would allow a lot of things that could make the program not reasonable. And erasure of course gives the ability to compatibly evolve a class from non-generic to generic, without breaking existing sources or binary class files.

Source: stackoverflow