java 是创建一个新对象并返回它还是在 return 语句中创建新对象更好?

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

Is it better to create a new object and return it or create the new object in the return statement?

java

提问by mheathershaw

For example:

例如:

public Person newPerson() {
  Person p = new Person("Bob", "Smith", 1112223333);
  return p;
}

as opposed to:

与:

public Person newPerson() {
  return new Person("Bob", "Smith", 1112223333);
}

Is one more efficient than the other?

一种比另一种更有效吗?

回答by Bozho

There isn't any difference that would make you chose one over the other in terms of performance.

没有任何区别会让您在性能方面选择其中之一。

  • For the sake of debugging, the former is better - i.e. when you want to trace the execution, or log some info between creation and returning.
  • For the sake of readability (but this is subjective) - the latter is better.
  • 为了调试,前者更好 - 即当您想跟踪执行时,或在创建和返回之间记录一些信息时。
  • 为了可读性(但这是主观的) - 后者更好。

In general, I'd advice for the latter, and whenever you need to debug this, make a temporary switch to the first (two-line) variant.

一般来说,我建议后者,每当您需要调试它时,临时切换到第一个(两行)变体。

回答by Ian C.

Assignment to a temporary variable before returning gives you a chance to do error checking and correction from within your newPerson(). Returning the new call requires the caller of your newPerson() method to catch and recover from errors.

在返回之前分配给临时变量使您有机会在 newPerson() 中进行错误检查和纠正。返回新调用需要 newPerson() 方法的调用者来捕获错误并从错误中恢复。

回答by mheathershaw

No one is not more efficient than the other, the JIT will inline it out to the most efficient at runtime.

没有一个比另一个更高效,JIT 会在运行时将其内联到最高效的状态。

回答by benzado

In a sane world, they will compile to exactly the same bytecode, so they are identical as far as performance is concerned. Then the only difference is for humans:

在一个理智的世界中,它们将编译为完全相同的字节码,因此就性能而言,它们是相同的。那么唯一的区别是对于人类:

Creating a temporary variablemakes debugging slightly easier: you can put a breakpoint on the return statement and inspect the value of p. In the one-line version, this is harder to do.

创建一个临时变量使调试稍微容易一些:您可以在 return 语句上放置一个断点并检查 的值p。在单线版本中,这更难做到。

Doing it in one lineeliminates a variable, reducing complexity, making the code slightly easier to read.

在一行中完成它消除了一个变量,降低了复杂性,使代码更容易阅读。

In practice, I would write it in one line, and in the worst case I create a temporary variable if I run into the debugging issue. Besides, aren't unit tests supposed to eliminate the need for debugging? :-)

在实践中,我会将它写在一行中,在最坏的情况下,如果遇到调试问题,我会创建一个临时变量。此外,单元测试不应该消除调试的需要吗?:-)

回答by polygenelubricants

One is not more efficient than the other, but returning created object directly is probably cleaner since you're using less temporary variables. If you must use temporary variables, make it finaland name it appropriately.

一个并不比另一个更有效,但直接返回创建的对象可能更干净,因为您使用的临时变量较少。如果您必须使用临时变量,请制作final并适当命名。

回答by Christopher Oezbek

The second option is shorter and easier to read.

第二个选项更短且更易于阅读。

I doubt that any decent Java compiler would create less efficient code for the first option though.

我怀疑任何体面的 Java 编译器都会为第一个选项创建效率较低的代码。