Is There Any Performance Reason to Declare Method Parameters Final in Java

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

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.

Why Final is used in method parameters

final is used in method parameters to make the references unchangeable after it is passed into the method. This is a specialized way of securing the passed parameters. so, the method receiving will not be able to re-initialize it with new object or value

Use of final keyword in Java method performance?

Theoretically, declaring a parameter final is not going to make a difference: the compiler is allowed to be smart enough to figure out that your method does not change the limit parameter, and optimize the code that it generates as if the parameter were declared final without the actual declaration.

The biggest difference that you are going to get by declaring method parameter final is the ability to reference that parameter in anonymous classes.

Another useful consequence is that people who maintain your code after you would know that keeping that parameter unchanged was your conscious decision, not a coincidence.

Compilation effects of declaring method parameters final

No there is not. This is purely a compile time check.

Note that bytecode is higher level then you seem to think - in particular there's no raw memory addresses. All parameter passing is abstracted away. Also, the Java compiler does very little optimization. The JIT is responsible for doing all the optimization at runtime.

Furthermore at the actual machine code level, passing a single word parameter by reference likely isn't going to optimize anything. Under normal circumstances, copying it will be faster (since it's in a register anyway). But again, this is all low level implementation details that the JIT worries about, not the purview of bytecode.

Does use of final keyword in Java improve the performance?

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...

how does it matter for methods to have 'final' parameters in Java?

Basically, the difference is between

public int doThing( int value )
{
value = value*2; // OK
return value;
}

and

public int doThing( final int value )
{
value = value*2; // Not OK
return value;
}

This can be helpful to you as a programmer to prevent you from changing the value accidentally.

There is one situation where the final keyword is necessary, and that is if you want to use the value in anonymous nested classes, e.g:

public Object makeThing( final String name )
{
return new Object()
{
@Override
public String toString(){
return name; // Won't work if `name` is not `final`.
}
};
}

Related:

  • Is there any performance reason to declare method parameters final in Java?
  • When should one use final for method parameters and local variables?
  • Using "final" modifier whenever applicable in java
  • Why would one mark local variables and method parameters as "final" in Java?
  • Why should I use the keyword "final" on a method parameter in Java?

final keyword in method parameters

Java always makes a copy of parameters before sending them to methods. This means the final doesn't mean any difference for the calling code. This only means that inside the method the variables can not be reassigned.

Note that if you have a final object, you can still change the attributes of the object. This is because objects in Java really are pointers to objects. And only the pointer is copied (and will be final in your method), not the actual object.

Does declaring variables as final, inside methods, improve performance?

No, final keyword on a local variable has no performance impact, and it cannot have even in theory, since .class files do not retain this information.

See this answer for details.

Why would one mark local variables and method parameters as final in Java?

You should try to do this, whenever it is appropriate. Besides serving to warn you when you "accidentally" try to modify a value, it provides information to the compiler that can lead to better optimization of the class file. This is one of the points in the book, "Hardcore Java" by Robert Simmons, Jr. In fact, the book spends all of its second chapter on the use of final to promote optimizations and prevent logic errors. Static analysis tools such as PMD and the built-in SA of Eclipse flag these sorts of cases for this reason.



Related Topics



Leave a reply



Submit