This probably looks duplicate of other questions on SO. But, I was wondering the way Java treat null.
Eg:
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));
}
}
Commenting Line 3 prints n whereas uncommenting throws NullPointerException. If I'm not wrong for Line 1 and Line 2, it being implicitly type cast to String. But, what happen to Line 3 ?
Integertoint.Line 2? Trying to concatenateIntegerandStringString. But, as you mentioned aboutIntegerthat it was unboxed toint. Then, in that case, It should displaynullnull0. right ? Because int default value is0.intis0, but you have noint.