为什么.class文件不可读? [英] Why is a .class file not human readable?

查看:185
本文介绍了为什么.class文件不可读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编译java文件时,会生成.class文件。现在这个.class文件具有JVM解释的字节码。当我们在文本编辑器中打开.class文件时,它不是人类可读的。现在要查看字节码,可以使用像javap这样的反汇编程序。

When a java file is compiled, it generates a .class file. Now this .class file has the bytecode which the JVM interprets. when we open the .class file in a text editor, it is not human readable. Now to view the bytecode a disassembler like javap can be used.

我的问题是,为什么我们需要反汇编字节码才能查看字节码本身?

My question is, why do we need to disassemble bytecode in order to view the bytecode itself?

反汇编程序实际上做了什么,将.class文件转换为人类可读的格式?

What does the disassembler actually do, to convert the .class file into human readable format?

推荐答案

Java虚拟机模拟机器。这就是它被称为 machine 的原因,尽管它是一个在硬件中不存在的虚拟。因此,在考虑 javap outout与实际Java字节代码的区别时,请考虑程序集和机器代码之间的区别:

The Java virtual machine simulates a machine. This is why it is called a machine, despite it being a virtual one that does not exist in hardware. Thus, when thinking about the difference of the javap outout and the actual Java byte code, think about the difference between assembly and machine code:

汇编代码使用所谓的助记符来使代码具有人类可读性。然而,这样的助记符名称与机器无关,因为机器只知道如何读取和操作二进制数据。因此,我们必须使用汇编程序来组装助记符(及其潜在参数),其中每个这样的助记符都被转换为其二进制等价物。例如,为了从特定寄存器加载一个值,我们会在汇编中写出类似 load 0xFF 的内容,而不是使用该指令的实际二进制操作码,这可能类似于 1001 1011 1111 1111 。类似地,对于Java字节代码,助记符是 javap 产生的,我们需要将二进制数据表示给它可以处理的(虚拟)机器。只有当我们想要读取字节代码时,我们才会将其反汇编为 javap 所代表的汇编代码。

Assembly code uses so-called mnemonics to make code human readable. Such mnemonic names are however nothing a machine can relate to because a machine only knows how to read and manipulate binary data. Thus, we have to assemble the mnemonic (and its potential arguments) using an assembler where each such mnemonic is translated into its binary equivalent. For example, for loading a value from a specific register we would write something like load 0xFF in assembly instead of using the actual binary opcode for this instruction which might be something like 1001 1011 1111 1111. Similarly, with Java byte code, the mnemonic being what javap produces, we need to represent binary data to the (virtual) machine which it is then is able to process. Only if we want to read the byte code, we rather disassemble it into the assembly code that javap represents.

请记住:唯一的汇编语言和 javap 输出存在的原因是你和我这样的人不喜欢阅读二进制代码。我们接受培训,以区分我们所看到的形状,例如字母和名称。相反,机器通过读取比特流来顺序地解释数据。如上所述,这些位很难让我们阅读,这就是为什么我们宁愿以十六进制格式呈现它们:而不是 1111 1111 ,我们宁愿写 0xFF 。但这仍然很难阅读,因为这样的数值并没有揭示其背景意义。 0xFF 仍然可能意味着一切。这就是为什么我们宁愿使用上面提到的这种含义隐含的助记符。

Keep in mind: The only reason that assembly language and the javap output exists is the fact that humans such as you and me do not enjoy reading binary code. We are trained to distinguish what we see by shapes as for example letters and names. In contrast, a machine interprets data sequentially by reading a stream of bits. As mentioned, these bits are hard for us to read which is why we rather present them in hexadecimal format: Instead of 1111 1111, we rather write 0xFF. But this is still rather difficult to read as such a numeric value does not reveal its contextual meaning. 0xFF could still mean about everything. This is why we rather use the mentioned mnemonics where this meaning is implicit.

你可能会认为虚拟机仍然是虚拟的,因此这台机器确实可以解释助记符而不是比二进制Java 字节代码。但是,这样的助记符会消耗更多空间(字符串当然只是由机器表示为字节),并且它还需要比在JVM上运行的模拟机器语言更多的时间来解释。因此,与标准编码(例如ASCII)相比,您可以将字节代码视为奇怪的编码,其中字符集仅包含字而不是字母,其中字仅是Java虚拟机使用和理解的字。显然,这个 Java字节代码字符集比使用ASCII描述类文件的内容更有效。

You might argue that a virtual machine is still only virtual and this machine could therefore indeed interpret mnemonics rather than binary Java byte code. However, such mnemonics would consume more space (strings are of course just represented as bytes by a machine) and it also take more time to interpret than the simulated machine language that is run on the JVM. You can therefore also think about the byte code being a weird encoding compared to standard encodings such as ASCII where the charset only contains words instead of letters where the words are only those that are used and understood by the Java virtual machine. Obviously, this Java byte code charset is more efficient than using ASCII for describing the contents of a class file.

这篇关于为什么.class文件不可读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆