aspose file tools*
The moose likes Java in General and the fly likes Final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Final variable" Watch "Final variable" New topic
Author

Final variable

Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi all,

I was asked once whether a final variable, method or class can significantly improve the efficiency of the application. If so, justify your answer. I thought well and said nothing. but the irony is I am still searching for the answer now. Can anybody help me out with good explanation?


Regards,
Abu.A
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
IMHO, final method and final class has nothing to do with performance . But final variables can improve performance since the variable will always refer to one object and you can change the object's properties (say creating a static final StringBuffer variable and using it in your methods). This way you aren't creating too many objects.But then, you should also take care of thread safety in such a scenario.

But this is my take.

Thanks,
Srikkanth

Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi Sri, Thanks for your answer. But i think I am not getting the point here. StringBuffer is mutable so you always dont create too many objects. You can manipulate properties of StringBuffer and use it in your methods. I dont see how final keyword plays a role here to improve performance.

Abu.A
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Hi Abubacker,
You may look at
this thread.

Same discussion happend here.

Life Rocks,
Tanzy.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Only final variables that are computed from primitives and String literals, the so-called compile time constants, will give you some performance gain. For instance:
This code will always look up the value for "s" whenever it is accessed. If you make "s" final though, the compiler can see that "s" will always have the same value, and it will replace every occurrence of "s" with that value: "My favourite number is 13".

My original code decompiled with JAD:
Now after decompiling the same code but with "s" being final:
But that's about as much as you gain in performance, and it really isn't much. You won't even notice anything.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Srikkanth Mohanasundaram
Ranch Hand

Joined: Feb 07, 2007
Posts: 185
Thanks Rob.
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110
Nice explanation Rob.
Thanks for sharing.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90

All the myth about final is just a wrong turn and even wrong design it seems. But the articles mentioned here says final is redundant sometimes in the application. Thanks for ALL.



Abu.A
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

IMO the final keyword is more about specifying intent rather than providing performance hinting. That said, who knows what compiler tricks, JIT tricks, etc. might be possible--but as others have said it's unlikely to make a *huge* difference in most situations.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Abubacker Siddik wrote:
I was asked once whether a final variable, method or class can significantly improve the efficiency of the application. If so, justify your answer. I thought well and said nothing. but the irony is I am still searching for the answer now. Can anybody help me out with good explanation?


In principle whenever you impose a limitation on your code you give more room for the compiler and runtime system to perform optimizations. So final potentially has an effect on performance.

But the most important reason for imposing limitations is to make your code safer (less error-prone) and easier to read. I think that should be your firts and major concern.

Anyway here's a thourough discussion of final,

http://oreilly.com/catalog/hardcorejv/chapter/ch02.pdf
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Is that your Final answer?

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Monu Tripathi wrote:Is that your Final answer?


You are asking me an answer? I wanted to know about importance of final variable or final keyword. Just got some suggestions. That's it dude.


Abu.A
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you read the link?
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

My dubious concern is how java handles modification to final variables. How does it treat an assignment and modification without assignment. Finally where does the altered valued is being stored.

For example



No doubt it prints 10 and 12. Is there any place where the values 12 is being stored and accessed.


Thanks & Regards
Sidharth Pallai
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Hi sidharth,

first of all you can't modify a final variable.

so the last line of your code is not modifying 'i', but its just adding 2 to the value 10 and printing it just as you do with any non final variable.


thanks


SCJP 1.6 96%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
from the above discussion I concluded that using final variables result in better performance.
does a final object reference variable affect performance?


thanks
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

Absolutely correct Neha,

But any idea, where does this new added value is being burnt , to be referenced later.

Like when one method passes as parameter the added final (apparently modified ) value to another method to receive and display.

For Eg.

Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
hi sidharth,

I guess it works just as it will work for any other non final variable.

when you write the declarartion statement 'int finalvalue' a memory space is allocated for the variable name finalvalue, its here where that value is being stored after passing it through the method.

thanks
 
wood burning stoves
 
subject: Final variable