• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Does final method really improves performance

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic