包含java.lang的目的是什么?对象在接口的常量池中?

时间:2022-01-19 17:20:21

Compiling the following interface:

编译如下界面:

package test;

public interface MyInterface {
    public void foo();
}

and checking the compiled code using javap -v -s test.MyInterface shows the following (-s prints member signatures):

并使用java -v -s测试检查编译后的代码。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
}

My question is: Why is there java.lang.Object in the constant pool, knowing that an interface does not inherit from the Object class?

我的问题是:为什么会有java.lang。对象在常量池中,知道接口不从对象类继承吗?

Also if I change the interface definition to:

如果我将接口定义更改为:

public interface MyInterface extends Comparable<MyInterface> {
    public void foo();
}

and run javap, I get the following:

运行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
  ...

What exactly is the purpose of including java.lang.Object in the signature Ljava/lang/Object;Ljava/lang/Comparable<Ltest/MyInterface;>; of the interface?

包含java.lang的确切目的是什么?对象在签名Ljava/lang/对象;Ljava/lang/类似的 ;接口的?

Also, if I try to view the bytecode using a tool (specifically JBE), it incorrectly shows that MyInterface has java.lang.Object as superclass with the class name java.lang.Object saved in the constant pool:

此外,如果我试图使用工具(特别是JBE)查看字节码,它会错误地显示MyInterface有java.lang。对象为具有类名java.lang的超类。在常量池中保存的对象:

包含java.lang的目的是什么?对象在接口的常量池中?

Note: Using jdk1.7.0_75

注意:使用jdk1.7.0_75

2 个解决方案

#1


8  

That Object class reference in the Constant Pool is the result of how the class file format has been defined in the Java VM Specification.

常量池中的对象类引用是Java VM规范中如何定义类文件格式的结果。

A class file consists of a single ClassFile structure:

类文件由一个类文件结构组成:

ClassFile {
    u4             magic;  // The Famous 0xCAFEBABE
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    ...
}

Regarding that super_class, this section of the JVM Specification is relevant for your MyInterface interface:

关于这个super_class, JVM规范的这一部分与MyInterface接口相关:

super_class

For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.

对于一个接口,super_class项的值必须始终是到constant_pool表的有效索引。该索引中的constant_pool条目必须是表示类对象的CONSTANT_Class_info结构。

So essentially, that java/lang/Object constant is needed only to fill the super_class item with a valid value. All Java instances are always instance of the basic Object class but this time the real answer has more to do with how the JVM was built and with specific implementation choices rather than with the language itself.

因此,本质上,只需要java/lang/对象常量就可以用一个有效值填充super_class项。所有Java实例都是基本对象类的实例,但这一次真正的答案更多地与JVM的构建方式以及特定的实现选择有关,而不是语言本身。

Also, as noted by @Holger, this paragraph is also worth mentioning:

此外,正如@Holger所指出的,这段话也值得一提:

If the value of the super_class item is zero, then this class file must represent the class Object, the only class or interface without a direct superclass.

如果super_class项的值为零,那么这个类文件必须表示类对象,这是唯一没有直接超类的类或接口。

#2


1  

Actually all in Java is Object. In Java each construction is object.

实际上,所有的Java都是对象。在Java中,每个构造都是对象。

Class IS Object
Interface IS Object
Enum IS Object.

So when you build program Object packed automatically. Because *.class may be used on another JVM.

当你构建程序对象时,它是自动打包的。因为*。类可以在另一个JVM上使用。

@user43250937 correctly in this case too.

@user43250937在这种情况下也是正确的。

JVM Spec :

Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format. The class file format precisely defines the representation of a class or interface, including details such as byte ordering that might be taken for granted in a platform-specific object file format

Java虚拟机要执行的编译代码使用与硬件和操作系统无关的二进制格式表示,通常(但不一定)存储在文件中,称为类文件格式。类文件格式精确地定义了类或接口的表示形式,包括一些细节,如在特定于平台的对象文件格式中可能被认为是理所当然的字节排序

May be this link granted to your more informations.

可能是这个链接授予您更多的信息。

Java-bytecode-fundamentals-using-objects-and-calling-methods

Java-bytecode-fundamentals-using-objects-and-calling-methods

Look at

看看

4:  invokeinterface #5,  1; //InterfaceMethod Job.execute:()Ljava/lang/Object;

JVM Specification

JVM规范

#1


8  

That Object class reference in the Constant Pool is the result of how the class file format has been defined in the Java VM Specification.

常量池中的对象类引用是Java VM规范中如何定义类文件格式的结果。

A class file consists of a single ClassFile structure:

类文件由一个类文件结构组成:

ClassFile {
    u4             magic;  // The Famous 0xCAFEBABE
    u2             minor_version;
    u2             major_version;
    u2             constant_pool_count;
    cp_info        constant_pool[constant_pool_count-1];
    u2             access_flags;
    u2             this_class;
    u2             super_class;
    ...
}

Regarding that super_class, this section of the JVM Specification is relevant for your MyInterface interface:

关于这个super_class, JVM规范的这一部分与MyInterface接口相关:

super_class

For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.

对于一个接口,super_class项的值必须始终是到constant_pool表的有效索引。该索引中的constant_pool条目必须是表示类对象的CONSTANT_Class_info结构。

So essentially, that java/lang/Object constant is needed only to fill the super_class item with a valid value. All Java instances are always instance of the basic Object class but this time the real answer has more to do with how the JVM was built and with specific implementation choices rather than with the language itself.

因此,本质上,只需要java/lang/对象常量就可以用一个有效值填充super_class项。所有Java实例都是基本对象类的实例,但这一次真正的答案更多地与JVM的构建方式以及特定的实现选择有关,而不是语言本身。

Also, as noted by @Holger, this paragraph is also worth mentioning:

此外,正如@Holger所指出的,这段话也值得一提:

If the value of the super_class item is zero, then this class file must represent the class Object, the only class or interface without a direct superclass.

如果super_class项的值为零,那么这个类文件必须表示类对象,这是唯一没有直接超类的类或接口。

#2


1  

Actually all in Java is Object. In Java each construction is object.

实际上,所有的Java都是对象。在Java中,每个构造都是对象。

Class IS Object
Interface IS Object
Enum IS Object.

So when you build program Object packed automatically. Because *.class may be used on another JVM.

当你构建程序对象时,它是自动打包的。因为*。类可以在另一个JVM上使用。

@user43250937 correctly in this case too.

@user43250937在这种情况下也是正确的。

JVM Spec :

Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format. The class file format precisely defines the representation of a class or interface, including details such as byte ordering that might be taken for granted in a platform-specific object file format

Java虚拟机要执行的编译代码使用与硬件和操作系统无关的二进制格式表示,通常(但不一定)存储在文件中,称为类文件格式。类文件格式精确地定义了类或接口的表示形式,包括一些细节,如在特定于平台的对象文件格式中可能被认为是理所当然的字节排序

May be this link granted to your more informations.

可能是这个链接授予您更多的信息。

Java-bytecode-fundamentals-using-objects-and-calling-methods

Java-bytecode-fundamentals-using-objects-and-calling-methods

Look at

看看

4:  invokeinterface #5,  1; //InterfaceMethod Job.execute:()Ljava/lang/Object;

JVM Specification

JVM规范