| Author |
Whether Using final With Local Variable Will Increase Performance?
|
Tanzy Akhtar
Ranch Hand
Joined: Jul 19, 2009
Posts: 110
|
|
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.
|
Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Tanzy Akhtar
Ranch Hand
Joined: Jul 19, 2009
Posts: 110
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
In those cases just follow Campbell's advice.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Monu Tripathi
Rancher
Joined: Oct 12, 2008
Posts: 1365
|
|
|
You can read this article written by Brian Goetz on usage of final keyword in code.
|
[List of FAQs] | [Android FAQ] | [My Blog] | [Samuh Varta]
|
 |
Tanzy Akhtar
Ranch Hand
Joined: Jul 19, 2009
Posts: 110
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
You're welcome
|
 |
 |
|
|
subject: Whether Using final With Local Variable Will Increase Performance?
|
|
|