Function or error: why is this Java code compiled?

Possible duplicate:
Is this valid Java?

I was surprised to find that the Java class below compiles. It has several methods with the same name, number of arguments, and types of argument erase types. However, it compiles and works as expected in windows using various versions of the Sun JDK 1.6 compiler. So if this is a mistake that exists for a long time ...

It is also compiled with numerous versions of Eclipse, but not the compiler with the compiler that ships with Eclipse 3.6

In addition, the calling code works as expected - i.e. There are no errors regarding the ambiguous methods of the calling code.

If you iterate over the methods returned by ErasureExample.class.getMethods (), all of them are present .....

According to JLS, this would be illegal if the methods had "overriding equivalent" signatures - they strictly do not, since none of the Collection, Collection or Collection overrides the equivalent ... if in this case Eclipse is wrong, the JDK is correct. ..

Function or error? Should it compile?

/** * Demonstrates that a class with methods that differ only by return type can exist. * Section 8.4 of the JLS suggests this is an error IFF the methods had * override equivalent signatures, which they dont'' * * * From JLS 8.4... * It is a compile-time error for the body of a class to declare as members two methods * with override-equivalent signatures (Β§8.4.2) (name, number of parameters, and types * of any parameters). * * Should it compile? */ public class ErasureExample { // note the single Collection<Integer> argument... public static int[] asArray(Collection<Integer> vals) { if (vals == null) return null; int idx = 0; int[] arr = new int[vals.size()]; for (Integer i : vals) { arr[idx] = i==null? 0 : i.intValue(); idx++; } return arr; } // same method name as above, type differs only by generics.... surely this violates 8.4 of JLS... public static long[] asArray(Collection<Long> vals) { if (vals == null) return null; int idx = 0; long[] arr = new long[vals.size()]; for (Long i : vals) { arr[idx] = i==null? 0 : i.longValue(); idx++; } return arr; } // same method name as above, type differs only by generics.... surely this violates 8.4 of JLS... public static boolean[] asArray(Collection<Boolean> vals) { if (vals == null) return null; int idx = 0; boolean[] arr = new boolean[vals.size()]; for (Boolean b : vals) { arr[idx] = b==null? false : b.booleanValue(); idx++; } return arr; } } 
+7
java generics type-erasure
source share
2 answers

The compiler is smart enough to disambiguate the methods at compile time, although at the end it performs type erasure. The whole point of parameterizing generics is to provide a security check such as compile time, erasing a type is just an implementation detail.

+2
source share

These methods do not have the same signature at all. Different return values ​​and different types of parameters. Yes, indeed, generics really matter. Everything looks good here.

-3
source share

All Articles