当我执行以下代码时,输出为“nullHelloWorld”。 Java如何处理null?
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String str=null;
str+="Hello World";
System.out.println(str);
}
}
最佳答案
您正在尝试将一个值连接到null
。这是由“字符串转换”控制的,当一个操作数是 String
时就会发生这种情况,并且由 the JLS, Section 5.1.11 涵盖。 :
Now only reference values need to be considered:
- If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).
关于java - 为什么字符串输出中出现空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53696198/