Compilers behave differently with a null parameter of a generic method

Posted by Eyal Schneider on Stack Overflow See other posts from Stack Overflow or by Eyal Schneider
Published on 2010-06-08T18:31:32Z Indexed on 2010/06/08 18:42 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

The following code compiles perfectly with Eclipse, but fails to compile with javac:

public class HowBizarre {
      public static <P extends Number, T extends P> void doIt(P value) {
      }

      public static void main(String[] args) {
            doIt(null);
      }
}

I simplified the code, so T is not used at all now. Still, I don't see a reason for the error. For some reason javac decides that T stands for Object, and then complains that Object does not conform to the bounds of T (which is true):

HowBizarre.java:6: incompatible types; inferred type argument(s) java.lang.Number,java.lang.Object do not conform to bounds of type variable (s) P,T

found : <P,T>void

required: void

       doIt(null);
           ^

Note that if I replace the null parameter with a non-null value, it compiles fine.

Which of the compilers behaves correctly and why? Is this a bug of one of them?

© Stack Overflow or respective owner

Related posts about java

Related posts about eclipse