Adding String to List

At run time, the generic type parameters are erased, so List<Integer> becomes List, and you can add any reference type to it.

If you change addToList(List list) to addToList(List<Integer> list), the compiler will prevent you from adding Strings to that list in this method.

Leave a Comment