When I first learned Java, I wanted to look at.class files. However, when I opened the.class file with notepad, I regretfully found that most of the files inside were garbled. When you open a.java file, everything is fine. So what went wrong?

Computer files are divided into character files and byte files

First of all, there are two types of files on computers: character files and byte (binary) files. The most commonly used character files are. TXT files. It directly stores characters that people can recognize, Chinese, English, symbols and so on.

But a computer can’t read human text. It can only recognize binary zeros or ones. You need to convert these characters into byte format, which is called encoding. Conversely, the process of translating bytes into characters according to certain rules is called decoding (or uncoding, decompilation).

Most of the files on a computer, such as movies, music, and images, cannot be saved in a simple character file format. So these files are saved as byte files, and multimedia processing software dictates how they should be viewed using.avi,.mp3,.mp4,.jpeg and other encoding formats. How it is encoded, how it should be decoded, in order to restore the contents of the file.

I’m curious what’s in the.class file!

Whether a.java file or a.txt file is a character file, because the text in it is something we can read and understand. But in order for the machine to understand the Java program we wrote, Javac takes over the task of compiling it and converting it into.class files.

Using Notepad to open a.class file is disappointing: most of it is garbled. Why is that? As stated before: it should be decoded as it was compiled. But the computer doesn’t know how Javac compiles it and what format it is in.

So when we use Notepad to look at.class bytecode files, most of what we get is unreadable due to encoding errors.

So if you want to get a glimpse into.class files: try some software that knows how Javac is compiled and decompiles it. Jd-gui is one such decompile tool: it accurately translates most of the content, but also provides the ability to output.class files backwards to.java files.

Attach a portal: A Brief analysis of the Principles of Java decompilation