In this case, yes, the parameter could be declared as Object[]. But. If the class is generic, type-safety
will be enforced at compile time.
A minor change to Fu's code:
|
Code:
|
public class Fubar3<U>
{
public int myMethod(U[] u)
{
return u.length;
}
public static void main(String[] args)
{
String[] strings = {"uno", "dos", "tres"};
new Fubar3<String> fubar3 = new Fubar3<String>();
System.out.println(fubar3.myMethod(strings));
Integer[] ints = {1, 2, 3};
System.out.println(fubar3.myMethod(ints)); // compile time error
}
} |
Don't know whether I made things more obscure.
There are two Generics tutorials on the Sun site, elementary and advanced. And the relevant section in the JLS is also worth reading.
db