Java中的null行为

Rav*_*avi 0 java null concatenation nullpointerexception

这可能看起来与SO上的其他问题重复.但是,我想知道Java如何对待null.

例如:

public class Work {
    String x = null;
    String y;
    Integer z;

    public static void main(String arg[]) {
        Work w = new Work();
        w.x = w.x + w.y; // work Line 1
        w.x = w.x + w.y + w.z; // work Line 2
        w.z = w.z + w.z; // NullPointerException Line 3
        System.out.println(w.x.charAt(4));
    }
}
Run Code Online (Sandbox Code Playgroud)

评论Line 3打印n而不是注释NullPointerException.如果我没有错Line 1Line 2,它被隐式类型转换为String.但是,发生了Line 3什么?

Mic*_*cha 5

拆箱发生.整数没有+运算符.它必须被取消装箱到int(原始).