问题
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)?
回答1:
do not believe the final keyword improves readablity.
Some people (such as myself) find excessive final
s decreases readability.
do not believe it significantly improves performance.
final
local variables does not improve performance.
回答2:
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
回答3:
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.
回答4:
The final keyword has two uses:
- declare a class or method as final in order to prevent subclassing/overrding
- 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).
回答5:
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.
来源:https://stackoverflow.com/questions/5573369/why-are-local-variables-not-declared-final-in-most-open-source-java-projects