为什么在大多数开源java项目中局部变量没有被声明为final?

发布于 2024-10-31 00:47:55 字数 242 浏览 1 评论 0原文

如果我查看 OpenJDK、Hibernate 或 Apache 中的 java 源代码,我还没有看到任何声明为 Final 的局部变量。

这表明一些最广泛使用的java软件库的开发人员:

  • 不相信final关键字可以提高可读性。

  • 不相信它显着提高了性能。

为什么 stackoverflow 上的大多数贡献者认为应该使用它(基于投票最高的回复)?

If I look at the java source source code in the OpenJDK or Hibernate or Apache I have yet to see any local variables declared final.

This suggests that the developers of some of the most widely used java software libraries:

  • do not believe the final keyword improves readablity.

  • do not believe it significantly improves performance.

Why do the majority of contrbuters on stackoverflow believe it it should be used (based on the highest voted responses)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鸠书 2024-11-07 00:47:55

可能是因为输入final这个词中的五个长字母很麻烦……为什么他们要

final int x;

在打字量是打字量两倍的情况下

int x;

经历写作的痛苦呢?

我们开发人员很懒,你知道...:P

Probably because it's a hassle to type in the five LONG letters in the word final... why would they go through the pain of writing

final int x;

when it's twice as much typing as

int x;

?

We developers are lazy, you know... :P

眼眸 2024-11-07 00:47:55

不要相信final关键字
提高可读性。

有些人(比如我自己)发现过多的 final 会降低可读性。

不太相信
提高性能。

final 局部变量不会提高性能。

do not believe the final keyword
improves readablity.

Some people (such as myself) find excessive finals decreases readability.

do not believe it significantly
improves performance.

final local variables does not improve performance.

梦萦几度 2024-11-07 00:47:55

据我所知,final 关键字对变量的运行时性能没有影响。

我相信它的主要目的是帮助您捕获错误。如果你知道某件事永远不会改变,你就会将其标记为这样。与我们尽可能使用注释的原因类似,任何时候我们可以用运行时错误换取编译时错误,我们都会这样做。当您处理错误时发现错误,并且它是新鲜的请注意,它并没有消失并损坏某人的数据,导致您失去客户,是的,这是一件非常好的事情。你得到编译错误,修复它,继续,你不会破坏夜间构建,是的,这些都是好事。

As far as I'm aware, the final keyword has no impact on the runtime performance of your variables.

I believe it's primary purpose is to assist you in the catching of bugs. If you know something is never going to change, you mark it as such. Similar to why we use annotations where we can, any time we can trade a runtime bug for a compile time error, we do. Finding an error when you're working on it, and it's fresh in your mind, and it hasn't gone and corrupted someone's data causing you to lose customers, yeah that's a very good thing. You get the compile error, you fix it, you move on, you don't break the nightly build, yeah those are good things.

冰魂雪魄 2024-11-07 00:47:55

Final 关键字有两种用途:

  1. 将类或方法声明为 Final 以防止子类化/覆盖
  2. 将变量声明为 Final 以防止更改它(分配新值)

情况 2 通常应用于成员变量,以便使对象不可变(至少部分)或方法参数,以防止意外分配。

对于局部变量(即方法范围而不是参数),通常不需要或不需要,因为这些变量可能会在方法内更改(否则您可能不需要它们,除了缓存方法范围的引用)。

The final keyword has two uses:

  1. declare a class or method as final in order to prevent subclassing/overrding
  2. declare a variable as final in order to prevent changing it (assigning a new value)

Case 2 is normally applied to member variables in order to make the object immutable (at least partly) or to method parameters in order to prevent accidential assignments.

In case of a local variable (i.e. method scoped and not a parameter), that's normally not necessary or wanted, since those variables are likely to be changed within the method (otherwise you might not need them, except to cache a reference for method scope).

梦幻之岛 2024-11-07 00:47:55

我怀疑声明局部变量final是否能够提高性能。由于final的存在,Java编译器已经被要求能够判断一个变量是否可以被多次赋值,或者是否可以不被初始化。因此,实际上声明局部变量为final并不会告诉编译器任何它还不知道的信息——这只是为了读者的利益。

现在是否有时会提高可读性,这是更主观的。在一段复杂的代码中,最好(向您自己或未来的读者)承诺变量仅写入一次。但简化代码可能会更好,这样无论如何都是显而易见的。

I doubt declaring a local variable final ever improves performance. By virtue of the existence of final, Java compilers are already required to be able to tell if a variable might be assigned more than once, or might not be initialized. Therefore, actually declaring a local as final doesn't tell the compiler anything it didn't already know--it's only for the benefit of the reader.

Now whether it sometimes improves readability, that's more subjective. In a complicated piece of code it can be nice to promise (to yourself, or to future readers) that a variable is only written once. But it might be nicer to simplify the code so that is readily apparent anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文