Why does a null value appear in string output?

后端 未结 4 1166
旧时难觅i
旧时难觅i 2020-12-04 02:49

When I execute the following code the output is \"nullHelloWorld\". How does Java treat null?

import java.util.*;
import java.lang.*;
import java.io.*;

/* N         


        
4条回答
  •  长情又很酷
    2020-12-04 03:20

    When you try to concat null through + operator, it is effectively replaced by a String containing "null".

    A nice thing about this is, that this way you can avoid the NullPointerException, that you would otherwise get, if you explicitly called .toString() method on a null variable.

提交回复
热议问题