Let's say I have a class with raw type declaration as List (list1). It's just a simple example:
public class Wildcards {
public boolean contains(List list1, List<?> list2){
/*for(Object element: list1) {
if (list2.contains(element)) {
return true;
}
}*/
list1.add("12sdf34"); //insert String
return false;
}
}
In list1 I insert String value. (If I use unbounded wildcards for list1 as for list2 it would be more secure and it would be compilation error). However here is a raw type.
Now let's use this method as following:
List<Integer> list1 = new ArrayList<Integer>();
List<Double> list2 = new ArrayList<Double>();
System.out.println("Contains? " + (new Wildcards()).contains(list1, list2));
System.out.println("List1 element: " + list1.get(0));
I will not get any errors and receive the following result:
Contains? false
List1 element: 12sdf34
Could anyone explain how it might be as I initialized list1 as List of Integers?