【发布时间】:2014-09-10 02:19:34
【问题描述】:
当我尝试打印未初始化的 static char 数组 时,它会给出运行时错误(空指针异常),而未初始化的 static int 数组 会给出空值。为什么?
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".
}
}
【问题讨论】:
-
很确定,因为
println()的字符数组版本被重载并尝试将数组打印为字符串,而int[]重载只是将其打印为数组。当使用char *或通用void *指针调用时,请参阅C++ 的std::cout::operator<<。 -
是的,这一切都与
println重载有关,与数组本身无关。int[]和char[]基本相同,只是int是 4 字节有符号数据,char是 2 字节无符号数据。
标签: java