0

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 ?

5
  • "But, what happen to Line 3 ? " Unboxing of Integer to int. Commented Jun 11, 2015 at 11:07
  • what about Line 2 ? Trying to concatenate Integer and String Commented Jun 11, 2015 at 11:08
  • You already know it, so what is your question? Commented Jun 11, 2015 at 11:09
  • It was my assumption that, it is being type cast to String. But, as you mentioned about Integer that it was unboxed to int. Then, in that case, It should display nullnull0. right ? Because int default value is 0. Commented Jun 11, 2015 at 11:12
  • Yes, the default of int is 0, but you have no int. Commented Jun 11, 2015 at 11:13

3 Answers 3

5

Unboxing happens. Integer doesn't have + operator. It has to be unboxed to int (primitive).

Sign up to request clarification or add additional context in comments.

Comments

2

String has a lot of special allowances in Java, for better or for worse. The specification dictates that null gets converted to the string "null" when converted to a string, e.g. by string concatenation (which implicitly calls toString() on other operands). So adding null to a string works by virtue of the language spec saying so.

Based on this your first two lines will result in

w.x = "null" + "null";
w.x = "nullnull" + "null" + "null";

In line 3 you get a different phenomenon, in this case auto-boxing and -unboxing. To ease the disconnect between primitive types and their respective wrapper classes you can apply arithmetic operators to wrapper classes, which prompt their values to be unboxed, operated on, and the result being boxed again. And this is where the NullReferenceException happens, because w.z is still null and thus cannot be unboxed. In essence:

w.z = w.z + w.z

is turned by the compiler into:

w.z = Integer.valueOf(w.z.intValue() + w.z.intValue());

and the call to intValue() is what fails because w.z is null here.

Prior to Java 5 this is what you had to write yourself if you wanted to have calculations on Integer objects.

Comments

0

In java, reference default value is null. When you declared z but not initialized, then it will take default value as null since z is a reference variable. Therefore you got NullPointerException.

There are 2 ways that you can avoid NullPointerException.

  1. Initialize z.

    public class Work {
        String x = null;
        String y;
        Integer z = 0;   
    
        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; // work Line 3 
            System.out.println(w.x.charAt(4));
        }
    }
    
  2. Use valueOf(int i) when you re assign.

    w.z = (null == w.z) ? 0 : Integer.valueOf(w.z.intValue() + w.z.intValue());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.