是否有任何性能原因在Java中声明方法参数final? [英] Is there any performance reason to declare method parameters final in Java?

查看:119
本文介绍了是否有任何性能原因在Java中声明方法参数final?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如下所示:

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

Versus :

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

假设 bar 仅在 foo()<中被读取且永不修改/ code>。

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

推荐答案

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

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.

关于方法本身的最终修饰符是否具有任何性能增益的大量争论,因为无论修饰符如何,方法都将在运行时由优化编译器内联。在这种情况下,它也应该仅用于限制方法的覆盖。

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.

这篇关于是否有任何性能原因在Java中声明方法参数final?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆