Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final variable

 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abubacker,
You may look at
this thread.

Same discussion happend here.

Life Rocks,
Tanzy.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 242
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.
 
Tanzy Akhtar
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Rob.
Thanks for sharing.
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the link?
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic