• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Final Variable

 
Ranch Hand
Posts: 1325
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is that says when we use final variable vitual machine works fast
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
declaring a variable as final helps enforce the principle of least privilege and such values might not be modified.
I think that virtual machine "works" fast in case when we declaring as final methods or/and classes due to static binding and inlining.

Regards,
Oleg.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Virtual machine (or in fact your application) MAY run fast, it is not the case always. Most of the time it gives performance benefits because as the variable is final, it can be used for compile time optimizations of the code. For example if you are doing

range = MAX_VALUE - MIN_VALUE


where MAX_VALUE and MIN_VALUE are final variables, then this can be calculated at compile time itself and hence gives a minor performance gain. It can scale further, it such things are repeatedly, say in a loop.
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a variable is final, the JVM may choose to cache its value in a register instead of retrieving the latest value each time. If a variable is final and either a primitive or a String, then the compiler may substitute its value all over the code (like Piyush) said. Therefore, yes, it may speed up your application. However, the differences are so minute that no human will ever notice it.

Therefore, use final only if required (e.g. for anonymous classes), or if you want to indicate that a value is never allowed to change. Don't do it for performance reasons. (Although the never allowing to change is mostly enough reason, especially with instance fields.)
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic