简体   繁体   中英

Why is Bytecode not human-readable?

I'm confused about a certain topic:

When you compile Java or Python, you get bytecode which will run on the respective VMs. In a previous question I had asked why, when you open a.pyc or.class file in a text editor, it appears as gibberish and not like readable bytecode (LOAD, STORE operations etc).

Now the answer I got at the time based around the argument of "That's like saying if you opened an.exe file and expected to see x86 assembly" and they made the analogy that bytecode that I've seen is the "assembly" version of the real bytecode which is not readable.

This would be okay and make sense if not for one thing. You can't compare an exe file to a bytecode file. An exe file is ALREADY compiled to machine code. A bytecode file is NOT. A bytecode file is fed to a VM which then interprets it (usually with JIT).

That means that whoever wrote the JVM for instance, (which is just a piece of software itself), would need to write a bytecode-interpreter. And I really doubt they wrote an interpreter to handle the following:

Java.class file:

在此处输入图像描述

I could be wrong and maybe they DID write an interpreter to handle this form of bytecode for some odd reason, but it doesn't seem likely. However, if the JVM handles the "assembly" version of the bytecode, then that would mean the cycle is

.java ->.class (unreadable) ->.class (readable right as it enters the JVM) There's almost a meaningless step in between.

I'm just really confused at this point.

They did write an interpreter for this form of bytecode. They read it as bytes, of course, not ASCII characters, which makes it more usable. But, for example, each instruction code takes only one byte, not eg five to write store .

The goal was to have something compact in memory usage, but not actually compiled to machine code that would be specific to only one device. Java bytecode is more or less its own form of machine code.

If you would like to read it, however, use the javap command to decompile it to a more readable form.

Bytecode is the "machine code" for a virtual machine. As such, it has much the same goals and restrictions as "real" machine code - compact, efficient decoding, etc.

The fact that bytecode is executed by a virtual machine rather than by a "real" machines is not particularly relevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM