• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Whether Using final With Local Variable Will Increase Performance?

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am a bit curious to know about using final keyword with local variables.
Will it increase the performance or it's just a good programming style to use final with local variable?

Thanks,
Tanzy.
 
Marshal
Posts: 76873
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it. Use the System.nanoTime method before and after a method call, and see whether it makes any difference.
Use the javap tool with the -c option to print out the bytecode, and see whether there is any difference.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the value for the final variable is a primitive or String literal, or a String that is created by appending primitives and String literals (a.k.a. compile time constants), those will be inlined. For example:
All three will have all their references replaced by their value. For instance, System.out.println(s2) will actually be compiled as System.out.println("13Hello481.0").

If the final variable is constructed using anything else, including the results of any method, then the above does not hold.
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob for the valuable response.

Well, if we are using final with local variable and the the value depends on runtime,
then how about performance?

For instance--



which one is better to use?


Thanks,
Tanzy.
 
Rob Spoor
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In those cases just follow Campbell's advice.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The general rule that most programmers SHOULD live by is this:

Write code that makes sense, that does what you need, and is simple.

ONLY then, if you have a performance issue, should you consider optimizing it. and then, use a tool to find out where the slowdowns are. 99.9% of the time, the slowdown is somewhere you are not looking, and you can spend hours changing one thing to gain practically nothing, while making a modest change somewhere else could give HUGE boosts.
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read this article written by Brian Goetz on usage of final keyword in code.
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you rosenberger for giving such a nice advice.

Thank you Ritchie for giving nice trick to chek performance issues.

Thank you Tripathi for giving such a nice link.

Thank you all for valuable answers and suggestions.
 
Campbell Ritchie
Marshal
Posts: 76873
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
ice is for people that are not already cool. Chill with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic