122

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

As in:

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

Versus:

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

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

5
  • I can't think of a reason why the compiler would care if you declared a method parameter final or not. But the real answer to this question is - write two functions, one with final parameters and one with regular parameters. Run them a million times each and see if there's any noticeable runtime difference. If you're worried about performance, it's very important to do some profiling work on your code and find out exactly what is slowing you down. It's almost certainly not what you would expect it to be :) Commented Nov 5, 2008 at 21:57
  • 1
    I would suggest you never write micro benchmarks. You do not know which optimization the JIT can do and when, and you would likely get a wrong idea of how it behave just doing a "simple test case" Commented Aug 18, 2011 at 9:45
  • well that may be, but no one here has written or suggested a microbenchmark... Commented Aug 19, 2011 at 0:29
  • 1
    @Mike Blandford's answer suggests using a micro-benchmark does it not? Commented Sep 12, 2011 at 12:46
  • Writing micro benchmarks in Java is something very very tricky Commented Apr 24, 2012 at 6:54

4 Answers 4

101

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.

Sign up to request clarification or add additional context in comments.

3 Comments

you'd think that final variables/params could be optimized as loop variables though...but a good compiler/runtime should be able to figure that out without final anyway...
I have seen Sun's compiler emit marginally shorter bytecode when the only difference between the two methods has been the "finality" of local variables. Micro-optimizations are a real thing, and compilers do actually make them. What really matters, of course, is what the JIT does with the bytecode, and local references lose their "finality" when compiled to bytecode. I think this is why there is so much speculation about final local variables: it's quite non-deterministic what the results are. However, using final locals can affect the bytecode -- for what it's worth.
As it is non deterministic, there really isn't any way to depend on the optimization either. Of course, a lot may have changed in the VM implementations since the original answer in 2008 ;-)
15

The only benefit to a final parameter is that it can be used in anonymous nested classes. If a parameter is never changed, the compiler will already detect that as part of its normal operation even without the final modifier. It's pretty rare that bugs are caused by a parameter being unexpectedly assigned - if your methods are big enough to need this level of engineering, make them smaller - methods you call can't change your parameters.

3 Comments

"It's pretty rare that bugs are caused by a parameter being unexpectedly assigned". It's more common than you think...
@RAY you might be right, actually I don't have any data (beyond my own experience) to back up that claim.
@DobesVandermeer to be more precise, that isn't really a "benefit to a final parameter". What you are describing is simply required syntax for any local variables (of which I am including parameters as) to be made visible to the local scope of the anonymous nested class.
-1

Compilers that operate after class loading, such as JIT compilers, can take advantage of final methods. Consequently, methods declared final could have some performance benefit.

http://www.javaperformancetuning.com/tips/final.shtml

Oh and another good resource

http://mindprod.com/jgloss/final.html

3 Comments

The question was about parameters being declared final, which has no performance impact.
On modern JIT'S (Hotspot) final does not have any (measurable) performance influence at all, whether applied to parameters or the class
The two articles you link both suggest that final is rather a semantic mark for the developers, and that the JIT compilers may use final (but as others pointed out, they don't really need that info). So final is rather semantic, which is in parallel with modern C/++ compilers ability to infer constness of variables and methods even if they are not marked const explicitly.
-1

Just one more point that to above that using non-final local variables declared within the method—the inner class instance may outlive the stack frame, so the local variable might vanish while the inner object is still alive

1 Comment

The mechanics of outer variable references in anonymous inner classes make this a non-issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.