gpt4 book ai didi

java - 理解java类文件

转载 作者:行者123 更新时间:2023-12-01 19:27:11 26 4
gpt4 key购买 nike

我遇到了this问题:

How can we identify whether a compilation unit is class or interface from a .class file?

给出的答案是“来自java源文件头”,并附有解释

The Java source file contains a header that declares the type of class or interface, its visibility with respect to other classes, its name and any superclass it may extend, or interface it implements.

但是我没有在任何标准文档中找到任何解释源文件头如何指示类或接口(interface)的内容。维基百科says以下是类文件中的魔数(Magic Number)(我发现它是相关的):

CAFEBABE became the class file format, and CAFEDEAD was the persistent object format.

有人可以向我指出详细解释类文件如何判断它是用于类还是接口(interface)以及类文件的其他详细信息的文档吗?

最佳答案

How can we identify whether a compilation unit is class or interface from a .class file?

根据Chapter 4. The class File Format Java Virtual Machine Specification的:

A class file consists of a single ClassFile structure:

ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
. . .

还有:

The value of the access_flags item is a mask of flags used to denote access permissions to and properties of this class or interface. The interpretation of each flag, when set, is:

Flag Name      Value   Interpretation
ACC_PUBLIC 0x0001 Declared public; may be accessed from outside its package.
ACC_FINAL 0x0010 Declared final; no subclasses allowed.
ACC_SUPER 0x0020 Treat superclass methods specially when invoked by the invokespecial instruction.
ACC_INTERFACE 0x0200 Is an interface, not a class.
. . .

因此,如果 2 字节 access_flags 值设置了第 9 位,则它是一个接口(interface),否则它是一个类。

关于java - 理解java类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61158318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com