Does Java optimize method calls via an interface which has a single implementor marked as final?

后端 未结 4 1117
名媛妹妹
名媛妹妹 2021-02-08 15:03

If I have a reference to a class and invoke a method on it, and the class or the method is final, my understanding is that the compiler or the JVM would replace the dynamic disp

4条回答
  •  长发绾君心
    2021-02-08 15:46

    AFAIK, the JVM can in-line up to two methods, the methods doesn't have to be final. It can do this if the methods are small and called often. If your code calls three or more methods, only the most commonly called methods will be invoked.

    Note 1: The JVM does care how many implementations there are, only how many are actually called.

    Note 2: The JVM can inline methods which didn't exist at compile time, its only the code available at runtime which matters.

提交回复
热议问题