小编Tom*_*tch的帖子

为什么这段代码没有抛出 nullPointerException?

代码片段如下。

我知道 String 的默认值是null并且它被分配给str. 我不明白的是为什么null在第一种情况下打印而不在其他情况下打印(如第二个代码片段)。

public class Case1{

    static String str;

    public static void main(String[] args){

        System.out.println(str);
    }
}

\\Code prints 'null' without quotes
Run Code Online (Sandbox Code Playgroud)
public class Case2{

    public static void main(String[] args){

        String a[][] = { {}, null };
        System.out.println(a[1][0]);
    }
}

\\Code throws nullPointerException
Run Code Online (Sandbox Code Playgroud)

任何解释将不胜感激。

java nullpointerexception

-1
推荐指数
1
解决办法
80
查看次数

标签 统计

java ×1

nullpointerexception ×1