views:

71

answers:

2

Hi All, I have this program in my eclipse. And if I compile this with JDK 1.5(build path -> configure build path ->java compilern ->JDK compliance -> compiler compliance level = 1.5), I get the type erasure error for method m1

I have an option of choosing compiler compliance level as 5 in the driop down (build path -> configure build path ->java compilern ->JDK compliance -> compiler compliance level = 5), I don't get this error.

I don't see any difference in this setting I select. But why do I get the error? whats the difference between 1.5 and 5

import java.util.*;   
class  TestWrapper   
{   
public static void main(String[] args) {   
            List<Number> ls1 = new ArrayList<Number>();   
            List<String> ls2 = new ArrayList<String>();   
            m1(ls1,ls2);   
        }   
        public static void m1(List<Integer> l1,List<Integer> l2){}               
      public static void m1(List<Number> l1,List<String> l2)  {}  

Thanks all for the answers Java :)

+1  A: 

Java's generics are implemented using a method called "type erasure". Basically, the compiler inserts some hidden casts and removes the type from the generic class's name. So once the code is compiled, List<Integer> becomes List. Same with List<Number> and List<String>. Meaning you'd end up with two public static void m1(List, List) methods.

This shouldn't work in any Java compiler; why it'd be allowed is a bit of a mystery to me.

cHao
Thanks for your answers. I don't know what JDK it is set to or infact I dont know how to find it? I thought when I select 5 as my compliance level I thought it was JDK 1.5 or JRE 5. Am I wrong somewhere? I am setting up an environment, and my collegues here have it as 5. I would want to set the compilance level as 1.5. What is this compilance level in eclipse build path->compiler? I am confused!!!
Java
Method overload selection is done at compile time, so what happens at runtime does not matter.
Tom Hawtin - tackline
Type erasure is done at compile time too -- and type erasure would give you two identical methods. That's not going to fly in any working Java compiler.
cHao
And BTW, overload selection may be done at compile time, but class loading and linking is done at *runtime*. *WAY after* type erasure is done. The JVM would need to know how to resolve `m1(List, List)`, as that's (a slight oversimplification of) how it's known in the class. And two identically named functions, with identical signatures, would make that either impossible or unpredictable, depending on whether your JVM is crap or not.
cHao
A: 

IMO You should get the error in any case. Can you check what JDK is configured against the compliance level "5"

naikus
I don't know what JDK it is set to or infact I dont know how to find it? I thought when I select 5 as my compliance level I thought it was JDK 1.5 or JRE 5. Am I wrong somewhere? I am setting up an environment, and my collegues here have it as 5. I would want to set the compilance level as 1.5. What is this compilance level in eclipse build path->compiler? I am confused!!!
Java