• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does final method really improves performance

 
Rahul Deshmukh
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
 
Peter Haggar
author
Ranch Hand
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
 
shashank shah
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!