import java.util.*;
public class GenericDemo4 {
public static void main(String r[]) {
Set s1 = new HashSet();
s1.add(0);
s1.add("1");
dostuff(s1);
}
static void dostuff(Set<Number> s) {
do2(s);
Iterator i = s.iterator();
while(i.hasNext()) System.out.println(i.next() + " ");
Object [] oa = s.toArray();
for(int x = 0; x<oa.length; x++)
System.out.println(oa[x] + " ");
System.out.println(s.contains(1));
}
static void do2(Set s2) { System.out.println(s2.size() + " "); }
}
On line 10 there is a type-safe set as a parameter for the method dostuff and it accepts non generic Set s1. How can it not flag an error for one of it's element being a string(s1.add("1"))? From what I understand, integer 0(s1.add(0)) is accepted since Integer IS-A Number but that's not the case for String so how come no error ? Sorry if it's a silly doubt.