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
posted
0
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
posted
0
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.
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.
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.
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
posted
0
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.
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
posted
0
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
posted
0
from the above discussion I concluded that using final variables result in better performance.
does a final object reference variable affect performance?
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
posted
0
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.