java 为什么对象实例(为空)上的 toString() 没有抛出 NPE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29513497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why toString() on an Object instance (which is null) is not throwing NPE?
提问by Snehal Masne
Consider below one :
考虑以下之一:
Object nothingToHold = null;
System.out.println(nothingToHold); // Safely prints 'null'
Here, Sysout must be expecting String. So toString() must be getting invoked on instance.
在这里,Sysout 必须期待字符串。所以 toString() 必须在实例上被调用。
So why null.toString() works awesome? Is Sysout taking care of this?
那么为什么 null.toString() 效果很好呢?Sysout 会处理这个问题吗?
EDIT : Actually I saw this weird thing with the append() of StringBuilder. So tried with Sysout. Both behave in the same way. So is that method also taking care?
编辑:实际上我在 StringBuilder 的 append() 中看到了这个奇怪的东西。所以尝试使用 Sysout。两者的行为方式相同。那么这种方法是否也适用?
回答by Eran
PrintWriter
's println(Object)
(which is the method called when you write System.out.println(nothingToHold)
) calls String.valueOf(x)
as explained in the Javadoc:
PrintWriter
的println(Object)
(这是您编写时System.out.println(nothingToHold)
调用的方法)调用,String.valueOf(x)
如Javadoc 中所述:
/**
* Prints an Object and then terminates the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes <code>{@link #print(String)}</code> and then
* <code>{@link #println()}</code>.
*
* @param x The <code>Object</code> to be printed.
*/
public void println(Object x)
String.valueOf(Object)
converts the null to "null":
String.valueOf(Object)
将空值转换为“空值”:
/**
* Returns the string representation of the <code>Object</code> argument.
*
* @param obj an <code>Object</code>.
* @return if the argument is <code>null</code>, then a string equal to
* <code>"null"</code>; otherwise, the value of
* <code>obj.toString()</code> is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj)
回答by Konstantin Yovkov
The PrintStream#println(Object s)
method invokes the PrintStream#print(String s)
method, which first checks is the if the argument is null
and if it is, then just sets "null"
to be printed as a plain String
.
该PrintStream#println(Object s)
方法调用该PrintStream#print(String s)
方法,该方法首先检查参数是否是null
,如果是,则设置"null"
为打印为普通String
.
However, what is passed to the .print()
method is "null"
as String
, because the String.valueOf(String s)
returns "null"
before the .print()
method being invoked.
但是,传递给.print()
方法的内容是"null"
as String
,因为在调用方法之前String.valueOf(String s)
返回。"null"
.print()
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
回答by Don Chakkappan
Here is documentation for println()
这里是文档 println()
Prints a string followed by a newline. The string is converted to an array of bytes using the encoding chosen during the construction of this stream. The bytes are then written to the target stream with write(int).
If an I/O error occurs, this stream's error state is set to true.
打印后跟换行符的字符串。使用在构建此流期间选择的编码将字符串转换为字节数组。然后使用 write(int) 将字节写入目标流。
如果发生 I/O 错误,则此流的错误状态设置为 true。
NULL
can convert to bytes.
NULL
可以转换为字节。
回答by raslan
Object nothingToHold = null;
System.out.println(nothingToHold != null ? nothingToHold : "null String or any thing else");
This will display output if the nothingToHold(Object) not equals to null, otherwise it prints the message as "null String or any thing else"
如果 nothingToHold(Object) 不等于 null,这将显示输出,否则将消息打印为“null String or anyother thing”