Idan Yadgar Idan Yadgar - 6 months ago 13
Java Question

Java Generics - method after type erasure

How will the following method look after applying type erasure?

static<T> void InsertAt0(List<T> mylist, T element) {
mylist.add(0, element);

be replaced with
? or maybe type-erasure is only about generics classes?


Just remove all of the <T>s, and then replace any T with Object:

static void InsertAt0(List mylist, Object element) {
   mylist.add(0, element);

Note that mylist above has been erased to its raw type equivalent: List<T> became just List.

If you had specified a wildcard bound for T, like <T extends Number>, then you would replace T with that bound instead of Object:

static void InsertAt0(List mylist, Number element) {