开发者

Undocumented String.compareTo(null) NPE?

开发者 https://www.devze.com 2023-03-30 11:32 出处:网络
The following little test throws an NPE: public class Test { public static voidmain(String[] args) { String a = \"a\";

The following little test throws an NPE:

public class Test {

    public static void  main(String[] args) {

        String a = "a";
        String b = null;

        System.out.println(a.compareTo(b));

    }

}

Yet, the Javadoc of compareTo() does not mention that the parameter cannot 开发者_StackOverflow社区be null. This is strange, since Javadocs usually mentions when parameters cannot be null.

Is this just a glitch in the documentation or is there a more fundamental reason/twist I am missing?


You can get some explanation when you look at the Comparable interface:

Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

0

精彩评论

暂无评论...
验证码 换一张
取 消