This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Does final method really improves performance  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
in c and c++ we have macro and inline functions.
In java u can not override final methods but My query is
Does in java final methods wroks like inline methods ?
Inline means the method call is replaced by method body at compile time so logically it improves the performance.
I would like to know if final method works like an inkine method up to what extend it really improves the performance.
Thanks in advance
 
author
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do no equate final with inlining. The runtime optimizer (Jit or Hotspot) will inline a method based on different factors. final is just one of those factors. However, because a method is final, does not guarantee that it will ever be inlined. It simply makes it a potential candidate for inlining. For example, if a method is 100 lines long and has 100 call sites, the runtime optimizer probably won't inline it even if it's final due to code bloat. However, it would probably inline it if the method is a one-line "getter".
In addition, Hotspot can inline methods that are public and non-final. If Hotspot can determine that a method is not overridden, it might decide to inline it. At a later time, if a class is dynamically loaded that overrides that public method, Hotspot will un-inline the method to ensure the proper behavior of the program.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Peter Haggar:
... The runtime optimizer (Jit or Hotspot) will inline a method based on different factors. final is just one of those factors. However, because a method is final, does not guarantee that it will ever be inlined. It simply makes it a potential candidate for inlining.


Also, note that same applies to inline in C++. Method declared inline may not be inlined by the compiler. It simply makes it a potential candidate for inlining.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!