开发者

Does Java has a compiler directive to inline a method?

开发者 https://www.devze.com 2023-03-24 18:31 出处:网络
Has Java like other languages a directive to inline a method at compile time or at JIT time? We have many methods that call only the super method. This make it clear that it was not forget to overrid

Has Java like other languages a directive to inline a method at compile time or at JIT time?

We have many methods that call only the super method. This make it clear that it was not forget to override the method.

For example you have override the equals methods but was use the same hashcode like super implementation. This make clear for a later developer th开发者_JAVA百科at the hashcode method was not forget to implements. The same is also valid for setter and getter or add and remove.

But the compiler should inline this method.


The Java compiler does very few optimisations at compile time.

Instead the JIT does most of the optimisations at runtime based on how the applciation is actually used. It can inline methods, even up to two "virtual" methods.


No java does not. It's not a meaningful concept on Java.

As for the discussion of hashCode versus equals, some IDE and style checkers will warn you about mistakes like this, but I don't see that it has anything to do with 'inline'.


I don't think this can be inlined during compilation in Java, as all functions in Java are virtual, and you don't know until runtime which method is actually called, it might be the one of a subclass.

However, I don't think you need to worry about these things, because the HotSpot compiler should be pretty good at optimizing code that gets called frequently, on-the-fly, as the application is running.


Marking a method as final gives the JIT compiler a big hint as to how it can treat this method. You cannot force it to inline the method, but giving the JIT more explicit information can only help.

0

精彩评论

暂无评论...
验证码 换一张
取 消