java - 为什么字符串输出中出现空值?

标签 java string null string-concatenation

当我执行以下代码时,输​​出为“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/

相关文章:

java - 从 ArrayList<Object> 转换为 ArrayList<Custom> 类会引发错误,而从 Object 转换为 Custom 则不会

SQL 服务器 : concatenate values and ignore null or blank values

python - 用其他数据帧替换数据帧中的空值

mysql - 在 MySQL 中选择 NULL 和 NOT NULL 值

java - Alfresco 浏览器和多线程

java - Oozie Java操作被杀死,然后由集群重新启动

java - 嵌入式 Jetty HTTP/2 不适用于 Firefox/Chrome,但似乎可以用于 Safari

string - R中的特定字符串拆分

java - Java 标准输出中的克罗地亚语字符

java - 为什么我们不能在java中通过charAt(index)在特定位置添加字符