char and int array difference

后端 未结 4 1119
傲寒
傲寒 2021-02-18 22:23

When I try to print the uninitialized static char array it gives run time error (Null pointer exception) whereas the uninitialized static int array

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-18 23:08

    These are two different methods, and their APIs describe the behavior fully.

    public void println(Object x) calls String.valueOf at first, which returns "null" if x is null.

    See:

    • http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#valueOf-java.lang.Object-
    • http://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println-java.lang.Object-

    public void print(char[] c) calls the print(char[] c) method which throws NullPointerException if c is null.

    See:

    • http://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#println-char:A-
    • http://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#print-char:A-

提交回复
热议问题