0

Please consider the following context (It is a snippet of Android code)

TextView tvTitle = findViewById(R.id.something);
// do stuff with 'tvtitle'

In this context suppose both final declaration and non-final declaration is okay and works fine.

My question is: Is there any performance advantage in declaring variables as final or not? I've seen in many places in codes that normal variables is defined as final. I know what the final key word does but I do not know performance consideration about this.

Any help would be highly appreciated. Thanks

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • 4
    The one who voted to close this question because it _asks us to recommend or find a tool/library/off-site resource_ is really dumb – BackSlash Apr 20 '14 at 14:32
  • @BackSlash I do not ask anyone to recommend or find a tool for me, I just ask about performance considerations – frogatto Apr 20 '14 at 14:38

2 Answers2

3

It depends on the JVM implemenation. With a final variable, the java virtual machine is free to bypass certain checks and operations that would be needed to ensure strictly correct execution for a non-final variable.

JVMs will implicitly final local variables not written to. They cannot do this for fields that are not private, as another class could be loaded that would write to them.

You should also consider that declaring a variable final where it should be helps prevent bugs that may occur where you try to write to it. Your compiler will be able to catch them sooner in this case.

All in all, for a sane JVM, it wouldn't hurt to declare as final.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • Thanks for your response, As I mentioned my codes will run on dalvik virtual machine not a JVM, Can you please tell me how is about dalvik? – frogatto Apr 20 '14 at 14:32
  • @ABFORCE Nearly the same. The conceptual idea of optimizing accesses to final fields and local variables still holds, as well as that of maintanibility. – nanofarad Apr 20 '14 at 14:33
  • "that is not necessarily the case with all JVMs" Any JVM that you would want to use would do that. – Paul Draper Apr 20 '14 at 15:05
  • @PaulDraper Not all processor architectures fall under the scope of openjdk and other more optimized JVMs. For Android, however, it may be true. – nanofarad Apr 20 '14 at 15:24
  • 1
    The final modifier of a local variable is not expressed in the byte code, it has no performance implications. The JVM will never know that it was final. Note this is different for fields. – Steve Kuo Apr 20 '14 at 16:07
  • @hexafraction I've checked [another answer](http://stackoverflow.com/a/7067220/1841194) which says that final or non-final only is used at compile time not run time. Can you please tell me what is correct your answer or that answer? – frogatto Apr 21 '14 at 08:41
1

The Java compiler is perfectly able to determine which variables are being changed and which are not (thus being able to enforce something like final in the first place).

There is no performance advantage to declaring final for local variables yourself. The only advantages are developer ones, e.g. being able to know that a variable doesn't change, and have the compiler verify this for you.


Note: There can be advantages in final non-private methods, classes, and member variables. This provides information the compiler cannot know only from that source code. (Actual effectiveness may vary.)

Paul Draper
  • 78,542
  • 46
  • 206
  • 285