在接口的常量池中包含 java.lang.Object 的目的是什么?
编译以下接口:
package test;
public interface MyInterface {
public void foo();
}
并检查编译的代码使用显示以下内容( 打印成员签名):javap -v -s test.MyInterface
-s
Compiled from "MyInterface.java"
public interface test.MyInterface
SourceFile: "MyInterface.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
Constant pool:
#1 = Class #7 // test/MyInterface
#2 = Class #8 // java/lang/Object
#3 = Utf8 foo
#4 = Utf8 ()V
#5 = Utf8 SourceFile
#6 = Utf8 MyInterface.java
#7 = Utf8 test/MyInterface
#8 = Utf8 java/lang/Object
{
public abstract void foo();
Signature: ()V
flags: ACC_PUBLIC, ACC_ABSTRACT
}
我的问题是:为什么在常量池中有java.lang.Object
,知道接口不是从Object
类继承的?
另外,如果我将接口定义更改为:
public interface MyInterface extends Comparable<MyInterface> {
public void foo();
}
并运行,我得到以下内容:javap
Compiled from "MyInterface.java"
public interface test.MyInterface extends java.lang.Comparable<test.MyInterface>
Signature: #7 // Ljava/lang/Object;Ljava/lang/Comparable<Ltest/MyInterface;>;
SourceFile: "MyInterface.java"
minor version: 0
major version: 51
flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT
...
在签名Ljava/lang/Object中包含java.lang.Object
的目的究竟是什么?Ljava/lang/Comparable<Ltest/MyInterface;>;
的界面?
另外,如果我尝试使用工具(特别是JBE)查看字节码,它会错误地将其显示为具有超类,其类名保存在常量池中:MyInterface
java.lang.Object
java.lang.Object
注意:使用jdk1.7.0_75