java System.out.println() 中的 toString() 方法是双重调用吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16570937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:20:29  来源:igfitidea点击:

toString() method within System.out.println() a double call?

javaprintingdoublecalltostring

提问by Andrew Campbell

A professor of mine once said that the following code should never be done:

我的一位教授曾经说过,永远不应该执行以下代码:

System.out.println(object.toString());

System.out.println(object.toString());

He said (and I believe cited "Effective Java") it causes a double call. Since the print statement calls the toString method of an object, it would be less efficient to have the toString method called twice. The preferred method would be to just use:

他说(我相信引用了“Effective Java”)它会导致双重调用。由于 print 语句调用对象的 toString 方法,因此调用两次 toString 方法效率较低。首选方法是仅使用:

System.out.println(object);

System.out.println(对象);

Obviously this way looks better in code and would save time. I will always do it like this no matter what, but my question is "Is this actually more EFFICIENT?". In looking through the PrintStreamdocumentation, the print method has been overloaded to take a String as the parameter (which would be the case if the toString method were called first). I am not seeing where that version of the print method calls the toString method of the inputted parameter and I don't believe it would make sense for it to do that.

显然,这种方式在代码中看起来更好,并且可以节省时间。无论如何我都会这样做,但我的问题是“这实际上更有效吗?”。在查看PrintStream文档时,print 方法已被重载以将 String 作为参数(如果首先调用 toString 方法就是这种情况)。我没有看到该版本的 print 方法调用输入参数的 toString 方法的位置,我认为这样做没有意义。

Also, sorry if this is a duplicate. I couldn't find any topics on it.

另外,对不起,如果这是重复的。我找不到任何关于它的主题。

采纳答案by dasblinkenlight

No, it is not more efficient -- precisely because of the overload that you mentioned. Moreover, a call of toStringon a Stringis extremely quick, so even without an overload the difference would not be measurable.

不,它不是更有效——正是因为你提到的过载。此外,调用toStringaString非常快,因此即使没有过载,也无法测量差异。

However, your professor is right about not making the call like System.out.println(object.toString());, but the reason is different: since the call is unnecessary, the readers of your code may get confused.

然而,你的教授没有像 那样调用是正确的System.out.println(object.toString());,但原因不同:由于调用是不必要的,你的代码的读者可能会感到困惑。

回答by Andy Thomas

Your examples call two different methods in PrintStream. Both call toString()at most once.

您的示例在 PrintStream 中调用了两种不同的方法。两者toString()最多调用一次。

  • The first method calls println(String x), which does not call x.toString() itself.
  • The second method calls println( Object x ), which leads to a call of x.toString() if x is not null.

However, there is a potential advantage to using System.out.println(object). If objectis null, this prints "null". The other statement throws a NullPointerException.

但是,使用System.out.println(object). 如果object为 null,则打印“null”。另一个语句抛出 NullPointerException。

回答by fGo

in a multithreading environment calling System.out.println is actually bad enough, even much worst than a unneeded call to toString. The "problem" exist because you have a synchorized call inside "println":

在多线程环境中调用 System.out.println 实际上已经够糟糕了,甚至比不必要的 toString 调用还要糟糕。存在“问题”是因为您在“println”中有一个同步调用:

public void println() {
newLine();
}

private void newLine() {
try {
    synchronized (this) {
    ensureOpen();
...
}

So, if you are trying to write efficient java, you could start by avoiding it. As an alternative you could use any of the different logging mechanism, e.g. http://www.slf4j.org/

因此,如果您正在尝试编写高效的 Java,您可以从避免它开始。作为替代方案,您可以使用任何不同的日志记录机制,例如http://www.slf4j.org/