gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:02:57 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com