在 Java 中声明方法参数 final 是否有任何性能原因?

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

Is there any performance reason to declare method parameters final in Java?

javaperformancefinal

提问by Kip

Is there any performance reason to declare method parameters final in Java?

在 Java 中声明方法参数 final 是否有任何性能原因?

As in:

如:

public void foo(int bar) { ... }

Versus:

相对:

public void foo(final int bar) { ... }

Assuming that baris only read and never modified in foo().

假设barfoo().

采纳答案by Robin

The final keyword does not appear in the class file for local variables and parameters, thus it cannot impact the runtime performance. It's only use is to clarify the coders intent that the variable not be changed (which many consider dubious reason for its usage), and dealing with anonymous inner classes.

final关键字不会出现在局部变量和参数的类文件中,因此不会影响运行时性能。它的唯一用途是澄清编码人员不更改变量的意图(许多人认为其使用原因可疑),以及处理匿名内部类。

There is a lot of argument over whether the final modifier on the method itself has any performance gain since the methods will be inlined by the optimizing compiler at runtime anyway, regardless of the modifier. In this case it should also only be used to restrict the overriding of the method.

关于方法本身的 final 修饰符是否有任何性能提升存在很多争论,因为无论如何,优化编译器都会在运行时内联这些方法,而不管修饰符如何。在这种情况下,它也应该仅用于限制方法的覆盖。

回答by branchgabriel

Compilers that operate after class loading, such as JIT compilers, can take advantage of final methods. Consequently, methods declared final could have some performance benefit.

在类加载后运行的编译器,例如 JIT 编译器,可以利用 final 方法。因此,声明为 final 的方法可能会带来一些性能优势。

http://www.javaperformancetuning.com/tips/final.shtml

http://www.javaperformancetuning.com/tips/final.shtml

Oh and another good resource

哦还有另一个好资源

http://mindprod.com/jgloss/final.html

http://mindprod.com/jgloss/final.html

回答by Dobes Vandermeer

The only benefit to a final parameter is that it can be used in anonymous nested classes. If a parameter is never changed, the compiler will already detect that as part of it's normal operation even without the final modifier. It's pretty rare that bugs are caused by a parameter being unexpectedly assigned - if your methods are big enough to need this level of engineering, make them smaller - methods you call can't change your parameters.

最终参数的唯一好处是它可以在匿名嵌套类中使用。如果参数从未更改,即使没有 final 修饰符,编译器也会将其检测为正常操作的一部分。由意外分配的参数引起错误的情况非常罕见 - 如果您的方法大到需要这种级别的工程,请将它们变小 - 您调用的方法无法更改您的参数。

回答by user1402866

I assume the compiler could possibly remove all private static final variables that has a primitive type, such as int, and inline them directly in the code just like with a C++ macro.

我假设编译器可能会删除所有具有原始类型(例如 int)的私有静态最终变量,并像使用 C++ 宏一样直接在代码中内联它们。

However, i have no clue if this is done in practice, but it could be done in order to save some memory.

但是,我不知道这是否在实践中完成,但可以这样做以节省一些内存。

回答by user666

Just one more point that to above that using non-final local variables declared within the method—the inner class instance may outlive the stack frame, so the local variable might vanish while the inner object is still alive

还有一点比使用在方法中声明的非最终局部变量更重要 - 内部类实例可能比堆栈帧存活时间更长,因此局部变量可能会在内部对象仍然存在的情况下消失