aspose file tools*
The moose likes Beginning Java and the fly likes Whether Using final With Local Variable Will Increase Performance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Whether Using final With Local Variable Will Increase Performance?" Watch "Whether Using final With Local Variable Will Increase Performance?" New topic
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: 38818
    
  23
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: 19693
    
  20

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 - OCEEJBD 6
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: 19693
    
  20

In those cases just follow Campbell's advice.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

You can read this article written by Brian Goetz on usage of final keyword in code.


[List of FAQs] | [Android FAQ] | [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: 38818
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Whether Using final With Local Variable Will Increase Performance?