gpt4 book ai didi

java - 传递 String str=null 时输出为 null

转载 作者:行者123 更新时间:2023-12-02 00:43:59 26 4
gpt4 key购买 nike

public class StringTest
{
public static void main(String... args)
{
String str=null;
System.out.println(str);
}
}

为什么这段代码显示为空?当我们将引用变量传递给 println() 方法时,它将调用 toString() 方法。对于String类,将调用toString()方法返回当前对象。 String str=null 表示不存在任何对象。

最佳答案

documentation报告System.out.println(String):

Prints a String and then terminate the line. This method behaves as though it invokes print(String) and then println().

documentation对于 print(String) 报告:

Prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

关于java - 传递 String str=null 时输出为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17366844/

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