Undocumented String.compareTo(null) NPE?

后端 未结 1 1104
广开言路
广开言路 2021-01-03 19:57

The following little test throws an NPE:

public class Test {

    public static void  main(String[] args) {

        String a = \"a\";
        String b = nul         


        
相关标签:
1条回答
  • 2021-01-03 20:25

    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 讨论(0)
提交回复
热议问题