When I try to print the uninitialized static char array it gives run time error (Null pointer exception) whereas the uninitialized static int array gives null value. Why?
public class abc {
static int arr[];
static char ch[];
public static void main(String[] args) {
System.out.println(ch); //it gives null pointer exception at run time
System.out.println(arr); //it gives output as "null".
}
}
A character array contains characters; an integer array contains integers. And assuming you know the difference between a character and an integer, that's all the explanation you need… that's about it.
char: The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. int: As the name suggests, an int variable is used to store an integer. float: It is used to store decimal numbers (numbers with floating point value) with single precision.
Character Arrays String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable.
char *array = "One good thing about music"; declares a pointer array and make it point to a constant array of 31 characters. char array[] = "One, good, thing, about, music"; declares an array of characters, containing 31 characters.
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.
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With