Item 23 ( Don't use raw types in new code ) of Effective Java claims that using raw types in java code is always dangerous
For instance, it claims that the following method is dangerous and unsafe
// Use of raw type for unknown element type - don't do this!
static int numElementsInCommon(Set s1, Set s2) {
int result = 0;
for (Object o1 : s1)
if (s2.contains(o1))
result++;
return result;
}
Although, I can see that doing any kind of write operation or class cast operation on s1 and s2 can lead to all kinds of exceptions, I dont understand why the above method is unsafe. Author Joshua Bloch goes on to recommend the following method instead.
// Unbounded wildcard type - typesafe and flexible
static int numElementsInCommon(Set<?> s1, Set<?> s2){
int result = 0;
for (Object o1 : s1)
if (s2.contains(o1))
result++;
return result;
}
In other words, if a method only uses the methods of Object.Class and does not do any modifications to the parameters that are passed, why is it bad?