char and int array difference

后端 未结 4 1118
傲寒
傲寒 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:01

    The answer exists in the PrintWriter source code (of which System.out is an instance).

    Start with the fact that the uninitialized arrays, as reference variables, are given the default of null.

    The println(char[]) (eventually) attempts to call .length on the passed in array. It's null, resulting in the NullPointerException. println(char[]) (eventually) calls write(char[]):

    public void write(char buf[]) {
        write(buf, 0, buf.length);
    }
    

    There is no overload of println matching int[], but there is a println(Object). There it (eventually) attempts String.valueOf, passing the null reference, so String.valueOf takes the null and returns the String "null". println(Object) calls print(Object):

    public void print(Object obj) {
        write(String.valueOf(obj));
    }
    
    0 讨论(0)
  • 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-
    0 讨论(0)
  • 2021-02-18 23:15

    System.out is an instance of PrintStream and this class has a few overloaded println methods. In your case:

    1. System.out.println(ch); is using public void println(char x[])
    2. System.out.println(arr); is using public void println(Object x) (there is no public void println(int[] x) method so the closest available type for int[] which println can use is Object).

    The second method is using

    String.valueOf(x);
    

    to get a string representation of the object we want to print and the code of the valueOf method looks like

    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
    

    so it is null-safe (will return the string "null" if the reference holds null).

    The first method is at some level using

    public void write(char cbuf[]) throws IOException {
        write(cbuf, 0, cbuf.length);
                     //    ^^^^^^^ this throws NPE
    }
    

    and because cbuf is null cbuf.length will throw NullPointerException because null doesn't have length (or any other) field.

    0 讨论(0)
  • 2021-02-18 23:25

    You are actually calling two separate, overloaded System.out.println() methods. public void println(char[] x) is overloaded as per the documentation. No specific overloading of println(Object x) exists for the int[] arrays.

    So, when println() is called on an integer array, public void println(Object x) is called. According to the Javadocs:

    This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

    Since the value of the string is null, the method prints null.

    The println() method works somewhat differently when it takes a character array as a parameter. Superficially, the method itself seems similar:

    Prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println().

    However, the print(char[]) method behaves quite differently in key ways:

    Prints an array of characters. The characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

    Thus, it calls a different method. The documentation for print(char[]) explicitly states that in your situation, your exception is thrown:

    Throws: NullPointerException - If [input parameter] s is null

    Hence, the cause of the difference in behavior.

    See Javadocs for more information about the println and print methods: http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#println()

    0 讨论(0)
提交回复
热议问题