Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is invoke virtual always dynamically bind?

Tags:

java

Today one of my friend told me that,if a instruction is invoke virtual it is always dynamically bind,whereas invoke special is always statically bind? is it true?

if yes then why does when a method with final keyword is also invoke virtual?

please clarify this doubt that i have

like image 859
user3239652 Avatar asked May 09 '26 20:05

user3239652


1 Answers

In essence, yes you are correct. The fun part was the introduction of Hotspot. Hotspot is essentially another compiler, and it knows which classes are currently loaded and even more scaringly it knows when new classes are loaded and can recompile with that updated knowledge on the fly. Hotspot may choose to perform optimizations that remove dynamic dispatch, using knowledge not available to javac. When Hotspot was added, the code in javac was simplified. Sun stripped out a lot of logic, mostly the optimization parts.

So to answer your question, javac does not pay a huge amount of attention to final keywords for methods. It defers to its bigger brother, Hotspot.

Oracle documents some of these optimizations performed by Hotspot here: https://wikis.oracle.com/display/HotSpotInternals/PerformanceTechniques. Have a look under the section for methods.

like image 63
Chris K Avatar answered May 12 '26 10:05

Chris K