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?

Stephen D
  • 2,836
  • 4
  • 27
  • 40
  • 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. – Luiggi Mendoza Aug 09 '13 at 15:42
  • 1
    This may also be highly dependent on the compiler as well as the JVM and JIT compiler. – nanofarad Aug 09 '13 at 15:42
  • 8
    I wish I could downevote Luiggi's comment – Steve Kuo Aug 09 '13 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? –  Aug 09 '13 at 16:22
  • 1
    I don't think this is a duplicate. This is concerned with using `final`s as arguments, whereas that is for regular variables. – Stephen D Aug 09 '13 at 16:27

3 Answers3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • 2
    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. – Steve Kuo Aug 09 '13 at 15:54
  • 2
    @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. – Joop Eggen Aug 09 '13 at 20:05
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.

SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257