invokeinterface의 요점은 무엇입니까?
JVM이 메서드를 호출하는 방법에 대한 이 기사 를 읽고 있는데 , 대부분의 내용을 가지고 있다고 생각합니다. 그러나 .NET Framework의 필요성을 이해하는 데 여전히 문제가 있습니다 invokeinterface
.
하나와 방법을 호출 할 때 나는 그것을 이해하는 방법은, 클래스는 기본적으로 방법의 가상 테이블을 가지고 있으며, invokevirtual
또는 invokeinterface
이 가상 테이블이 협의됩니다.
그렇다면 인터페이스에 정의 된 메서드와 기본 클래스에 정의 된 메서드의 차이점은 무엇입니까? 왜 다른 바이트 코드인가?
지침의 설명 도 매우 비슷합니다.
이 기사는 인터페이스의 메소드 테이블이 메소드가 호출 될 때마다 "다른 오프셋"을 가질 수 있다고 주장하는 것 같습니다. 내가 이해하지 못하는 것은 어떤 객체도 실제 유형으로 인터페이스를 가질 수 없기 때문에 인터페이스에 메소드 테이블이 전혀없는 이유입니다.
내가 무엇을 놓치고 있습니까?
각 Java 클래스는 클래스 의 각 메소드의 바이트 코드에 대한 "링크"를 포함 하는 가상 메소드 테이블 과 연관됩니다 . 해당 테이블은 특정 클래스의 수퍼 클래스에서 상속되며 하위 클래스의 새 메서드와 관련하여 확장됩니다. 예 :
class BaseClass {
public void method1() { }
public void method2() { }
public void method3() { }
}
class NextClass extends BaseClass {
public void method2() { } // overridden from BaseClass
public void method4() { }
}
결과 테이블
BaseClass 1. BaseClass / method1 () 2. BaseClass / method2 () 3. BaseClass / method3 () 다음 수업 1. BaseClass / method1 () 2. NextClass / method2 () 3. BaseClass / method3 () 4. NextClass / method4 ()
가상 메소드 테이블이 테이블의 NextClass
항목 순서 를 유지하고 대체 BaseClass
하는 "링크"를 덮어 쓰는 방법에 유의하십시오 method2()
.
따라서 JVM의 구현은 이 메소드가 호출 될 모든 오브젝트의 가상 메소드 테이블에서 항상 세 번째 항목이 invokevirtual
됨을 기억 하여 호출을 최적화 할 수 있습니다 BaseClass/method3()
.
함께 invokeinterface
이 최적화 할 수 없습니다. 예 :
interface MyInterface {
void ifaceMethod();
}
class AnotherClass extends NextClass implements MyInterface {
public void method4() { } // overridden from NextClass
public void ifaceMethod() { }
}
class MyClass implements MyInterface {
public void method5() { }
public void ifaceMethod() { }
}
이 클래스 계층은 가상 메소드 테이블을 생성합니다.
AnotherClass 1. BaseClass / method1 () 2. NextClass / method2 () 3. BaseClass / method3 () 4. AnotherClass / method4 () 5. MyInterface / ifaceMethod () 내 수업 1. MyClass / method5 () 2. MyInterface / ifaceMethod ()
As you can see, AnotherClass
contains the interface's method in its fifth entry and MyClass
contains it in its second entry. To actually find the correct entry in the virtual method table, a call to a method with invokeinterface
will always have to search the complete table without a chance for the style of optimization that invokevirtual
does.
There are additional differences like the fact, that invokeinterface
can be used together with object references that do not actually implement the interface. Therefore, invokeinterface
will have to check at runtime whether a method exists in the table and potentially throw an exception. If you want to dive deeper into the topic, I suggest, e.g., "Efficient Implementation of Java Interfaces: Invokeinterface Considered Harmless".
Comparing both instructions in the JVM Spec, the very first difference is that invokevirtual
checks the accessibility of the method during the lookup, while invokeinterface
doesn't.
참고URL : https://stackoverflow.com/questions/1504633/what-is-the-point-of-invokeinterface
'development' 카테고리의 다른 글
reactjs에서 문서 키 누르기 듣기 (0) | 2020.11.18 |
---|---|
Python에 어떤 리팩토링 도구를 사용합니까? (0) | 2020.11.18 |
hg 태그와 hg 북마크의 차이점은 무엇입니까? (0) | 2020.11.18 |
구성 스크립트는 어떻게 만듭니 까? (0) | 2020.11.18 |
AngularJS가 디렉티브 템플릿 내부의 DOM 요소에 액세스 (0) | 2020.11.18 |