public class Generic<T> {
public static void main(String[] args) {
Generic g = new Generic();
List<Integer> integerList = new ArrayList<>();
integerList.add(42);
g.f(integerList);
System.out.println(integerList);
}
void f(List<String> stringList){
stringList.add("hello");
}
}
Executing this code will COMPILE and print [42, hello]
I'm passing my List<Integer> integerList
to a List<String> stringList
and the compiler is fine with it. Why? Isn't the compiler suppose to check this sort of things? Line with g.f(integerList);
should throw a compile error. It's not.