3

Does the use final in method arguments allow the compiler oor runtime environment to work faster? For example, if you have a variable to pass to a method that you know will not be modified and be used as is, is it more efficient to declare it final?

Example: The first method should be faster than the second method

public int isLargerAfterTripledFaster(int num, final int limit) {
    num *= 3;
    return (num > limit);
}

public int isLargerAfterTripled(int num, int limit) {
    num *= 3;
    return (num > limit);
}

If I can be sure I will never want to pass a modifiable variable here, should I do this technique?

5
  • 1
    AFAIK the usage of final modifier can optimize this, but you're saving so few milliseconds here that is not worth it. If you really want to improve the performance of your application, use a profiler that will detect the real bottlenecks. Commented Aug 9, 2013 at 15:42
  • 1
    This may also be highly dependent on the compiler as well as the JVM and JIT compiler. Commented Aug 9, 2013 at 15:42
  • 8
    I wish I could downevote Luiggi's comment Commented Aug 9, 2013 at 15:55
  • 2
    There are plenty of reasons to use or not use final that have nothing to do with performance. Why the focus on performance? Commented Aug 9, 2013 at 16:22
  • 1
    I don't think this is a duplicate. This is concerned with using finals as arguments, whereas that is for regular variables. Commented Aug 9, 2013 at 16:27

3 Answers 3

9

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.

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

1 Comment

Which compiler? Can you back up your answer with a source?
3

The current java compilers do already a good data flow analysis, it is a statement of having a non-alterable parameter. Only dumb compilers could have some use for this.

For a reader however it is a good hint. And code is written once, read often.

In general it is enforced by some style guides, saying "a parameter should never be overwritten".

A better reason is the usage in an inner class, as there the method context requires parameters and local variables to be final.

However a final method; one that cannot be overriden has optimizing potential.

public final int isLargerAfterTripled(int num, int limit) { ... }

The compiler may inline the function code, as the method will never be overriden.

2 Comments

A non-overridden non-final method is just as likely to be inlined as a final method. final is not considered by the JIT compiler.
@SteveKuo yes on JIT level, at runtime. Yet a statically optimizing compiler may fill the function body in at its calls, for public final and private (=final) functions. IF the function starts with a check on null for a parameter, that might often be resolved etc.
3

final has absolutely no impact on performance. The JIT compiler does not consider final at all.

Also take a look at Brian Goetz's article on Java final.

2 Comments

The given link is no longer valid.
Link works fine for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.