Same class at run time, but declarations keep their generics
The objects a and b don't know String or Integer. The field declaration and the anonymous subclass's superclass do, because they live in class-file metadata.
Java checks generic types at compile time and then erases them: T becomes its bound (usually Object), casts are inserted where values come out, and bridge methods keep overriding working. That's why new T(), new T[n], instanceof List<String> and overloading on List<String> vs List<Integer> are impossible.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write a class Registry<T> whose constructor takes a Class<T> and whose add(Object o) method accepts the object only if type.isInstance(o) is true. Add a string and an integer to a Registry<String> and print what's stored.
Write a generic Pool<T> that takes a Supplier<T> and creates objects on demand with borrow(); returned objects go back with giveBack(T) and are reused. Show that the second borrow after a give-back reuses the object.
Write a program that shows List<String> and List<Integer> have the same class, and that reads the generic type of a method's return value static Map<String, Integer> counts() through reflection.
Explain it without notes
What is type erasure, step by step, and why did Java choose it?
What is a bridge method? Show when the compiler generates one.
List the things you can't do with a type parameter because of erasure, and a workaround for each.
What is heap pollution, and how can it cause a ClassCastException on a line with no cast?
If types are erased, how do frameworks like Jackson know to deserialize into List<Order>?
Expected output
same class: true
class name: java.util.ArrayList
field type: java.util.List<java.lang.String>
type params of ArrayList: [E]
captured: java.util.Map<java.lang.String, java.util.List<java.lang.Integer>>
instanceof List<?>: true