Recommend an idea artifact! One click View of Java bytecode and other class information

Time:2021-10-16

Since a plug-in to be recommended in this article is used in the article to be shared later, let’s share it here. Very practical! You’ll love it!

Recommend an idea artifact! One click View of Java bytecode and other class information

Before we begin to recommend idea bytecode viewing artifact, let’s review what Java bytecode is.

What is Java bytecode?

A Java virtual machine (JVM) is a virtual machine that runs Java bytecode. The JVM has specific implementations for different systems (windows, Linux, MacOS) to use the same bytecode, and they will all give the same results.

What is bytecode? What are the benefits of bytecode?

In Java, the code that the JVM can understand is calledBytecode(that is, the extension is).classIt does not target any specific processor, only virtual machines. By means of bytecode, Java language solves the problem of low execution efficiency of traditional interpretative language to a certain extent, and retains the portability of interpretative language. Therefore, the Java program runs more efficiently. Moreover, because the bytecode is not specific to a specific machine, the Java program can run on computers with different operating systems without recompiling.

Java programs generally have the following three steps from source code to operation:

Recommend an idea artifact! One click View of Java bytecode and other class information

Why view Java bytecode?

When we study at ordinary times, we often need to view the bytecode file of a Java class. Viewing bytecode files makes it easier for us to understand the principles behind java code, such as the essence of various syntax sugars in Java.

How to view Java bytecode?

If we view the bytecode file of a class through the command line, we can directlyjavapCommand, but this method has begun. It is very inefficient and doesn’t look intuitive.

The following describes two ways to view bytecode files corresponding to classes using idea (_javapThis way will not be mentioned).

Let’s take this code as an example:

public class Main {
    public static void main(String[] args) {
        Integer i = null;
        Boolean flag = false;
        System.out.println(flag ? 0 : i);
    }
}

The above code caused a strange NPE exception due to improper use of the ternary operator. To find out why, let’s look at the corresponding bytecode.

Use the built-in function of idea

We clickView -> Show BytecodeYou can view the bytecode file corresponding to a class through idea.

It should be noted that:Before viewing the bytecode file corresponding to a class, make sure it has been compiled.

Recommend an idea artifact! One click View of Java bytecode and other class information

After a few seconds, you can intuitively see the bytecode content of the corresponding class.

Recommend an idea artifact! One click View of Java bytecode and other class information

As you can see from the bytecode, the position I circled has changedUnpacking operation

  • Packing: wrap basic types with their corresponding reference types;
  • Unpacking: convert package type to basic data type;

The following is a detailed explanation:flag ? 0 : iIn this line of code, 0 is the basic data type int. when returning data, I will be forcibly unpacked into int type. Since the value of I is null, an NPE exception is thrown.

Integer i = null;
Boolean flag = false;
System.out.println(flag ? 0 : i);

If, we put the codeflagIf the value of the variable is modified to true, there will be no NPE problem, because 0 will be returned directly and unpacking operation will not be carried out.

Use the idea plug-in jclasslib (recommended)

Compared with the function of viewing class bytes provided by idea, I recommend itjclasslibThis plug-in is great!

usejclasslibYou can view not only the bytecode file corresponding to a class, but also the basic information, constant pool, interface, attribute, function and other information of the class.

Recommend an idea artifact! One click View of Java bytecode and other class information

We can find this plug-in directly in the plug-in market of idea. I’ve installed it here.

Recommend an idea artifact! One click View of Java bytecode and other class information

After installation, restart idea. clickView -> Show Bytecode With jclasslibCan passjclasslibView the bytecode file corresponding to a class.

Recommend an idea artifact! One click View of Java bytecode and other class information

My open source project recommendation

  1. JavaGuide: “java learning + interview guide” covers the core knowledge that most Java programmers need to master. Prepare for a Java interview, the preferred javaguide!
  2. guide-rpc-framework: a custom RPC framework implemented by netty + kyro + zookeeper
  3. jsoncat: a lightweight HTTP framework that imitates spring boot but is different from spring boot
  4. programmer-advancement: programmers should have some good habits + interview must know!
  5. springboot-guide: not only spring boot but also important knowledge of spring
  6. awesome-java: collection of awesome java project on GitHub

I sorted out a high-quality original PDF resource to share with you for free. Most of the content is my original, and a small part comes from friends.

<img style=”zoom:50%;” />

<img style=”zoom:50%;” />

Download address:https://cowtransfer.com/s/fbed14f0c22a4d

I’m brother guide, a Java back-end developer, a little front-end, free boy. See you next time! Wechat search“JavaGuide”Reply“Interview assault”Get 4 original PDFs I sorted out