Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does use of final keyword in Java improve the performance?

In Java we see lots of places where the final keyword can be used but its use is uncommon.

For example:

String str = "abc"; System.out.println(str); 

In the above case, str can be final but this is commonly left off.

When a method is never going to be overridden we can use final keyword. Similarly in case of a class which is not going to be inherited.

Does the use of final keyword in any or all of these cases really improve performance? If so, then how? Please explain. If the proper use of final really matters for performance, what habits should a Java programmer develop to make best use of the keyword?

like image 831
Abhishek Jain Avatar asked Nov 25 '10 17:11

Abhishek Jain


People also ask

What is the benefits of using final in Java?

Final keyword improves performance. Not just JVM can cache the final variables but also applications can cache frequently use final variables. 2. Final variables are safe to share in a multi-threading environment without additional synchronization overhead.

When should we use final keyword in Java?

What is the Final Keyword in Java? Java final keyword is a non-access specifier that is used to restrict a class, variable, and method. If we initialize a variable with the final keyword, then we cannot modify its value. If we declare a method as final, then it cannot be overridden by any subclasses.

Is final keyword useful?

The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override). The final keyword is useful when you want a variable to always store the same value, like PI (3.14159...). The final keyword is called a "modifier".

What are the 3 uses of final keyword in Java?

Final keyword in Java has three different uses: create constants, prevent inheritance and prevent methods from being overridden.


1 Answers

Usually not. For virtual methods, HotSpot keeps track of whether the method has actually been overridden, and is able to perform optimizations such as inlining on the assumption that a method hasn't been overridden - until it loads a class which overrides the method, at which point it can undo (or partially undo) those optimizations.

(Of course, this is assuming you're using HotSpot - but it's by far the most common JVM, so...)

To my mind you should use final based on clear design and readability rather than for performance reasons. If you want to change anything for performance reasons, you should perform appropriate measurements before bending the clearest code out of shape - that way you can decide whether any extra performance achieved is worth the poorer readability/design. (In my experience it's almost never worth it; YMMV.)

EDIT: As final fields have been mentioned, it's worth bringing up that they are often a good idea anyway, in terms of clear design. They also change the guaranteed behaviour in terms of cross-thread visibility: after a constructor has completed, any final fields are guaranteed to be visible in other threads immediately. This is probably the most common use of final in my experience, although as a supporter of Josh Bloch's "design for inheritance or prohibit it" rule of thumb, I should probably use final more often for classes...

like image 193
Jon Skeet Avatar answered Sep 25 '22 14:09

Jon Skeet