When I try to print the uninitialized static char array it gives run time error (Null pointer exception) whereas the uninitialized static int array
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));
}
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:
public void print(char[] c)
calls the print(char[] c) method which throws NullPointerException if c is null.
See:
System.out
is an instance of PrintStream
and this class has a few overloaded println
methods. In your case:
System.out.println(ch);
is using public void println(char x[])
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.
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()