Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

IDEA one-click how to view Java bytecode and other class information plug-ins

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This issue of the content of the editor will bring you about the IDEA button how to view Java bytecode and other types of information plug-ins, the article is rich in content and professional analysis and description for you, I hope you can get something after reading this article.

Before you start recommending IDEA bytecode viewing artifacts, let's review what Java bytecodes are.

What is Java bytecode?

The Java virtual machine (JVM) is a virtual machine that runs Java bytecode. JVM has a specific implementation (Windows,Linux,macOS) for different systems, which aims to use the same bytecode, and they all give the same result.

What is a bytecode? What are the benefits of using bytecode?

In Java, the code that JVM can understand is called a bytecode file), which is not targeted at any particular processor, only for virtual machines. Java language solves the problem of low execution efficiency of traditional interpretive language to some extent by means of bytecode, while retaining the portability of interpretive language. So Java programs run efficiently, and because bytecode is not specific to a particular machine, Java programs do not need to be recompiled to run on computers with many different operating systems.

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

Why look at Java bytecodes?

When we are usually learning, we often need to look at the bytecode file of a certain java class. Looking at bytecode files makes it easier for us to understand the principles behind java code, such as the nature of various syntax sugars in java.

How do I view Java bytecodes?

If we look at a class bytecode file from the command line, we can use the javap command directly, but this is too primitive, inefficient, and doesn't look intuitive.

Here are two ways to use IDEA to view the class corresponding bytecode file (not to mention javap).

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 causes a weird NPE exception due to improper use of the ternary operator. To figure out why, let's look at the corresponding bytecode.

Use IDEA built-in features

Click View-> Show Bytecode to view the bytecode file corresponding to a class through IDEA.

It is important to note that before looking at the bytecode file for a class, make sure it has been compiled.

After a few seconds, you can visually see the bytecode contents of the corresponding class.

It can be seen from the bytecode that the unpacking operation has taken place in the position I circled.

Boxing: wrap basic types with their corresponding reference types

Unpacking: converting packaging types to basic data types

The detailed explanation is: flag? 0: I in this line of code, 0 is the basic data type int. When the data is returned, I will be forced to unbox to int type. Because the value of I is null, a NPE exception is thrown.

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

If we change the value of the flag variable in the code to true, there will be no NPE problem, because 0 will be returned directly and the unboxing operation will not be carried out.

Use the IDEA plug-in jclasslib (recommended)

Compared to the function of viewing class bytes that comes with IDEA, I recommend jclasslib as a plug-in. It's great!

Using jclasslib, you can not only visually view the bytecode file corresponding to a class, but also view the basic information of the class, constant pool, interface, properties, functions and other information.

We can find this plug-in directly in the IDEA plug-in market. I've already installed it here.

After the installation is complete, restart IDEA. Click View-> Show Bytecode With jclasslib to view the bytecode file corresponding to a class through jclasslib.

Postscript

Work needs, a survey of the desktop development of some commonly used technology stack, share it, for the needs of small partners for reference.

The above is the IDEA button for everyone to share how to view Java bytecode and other types of information plug-ins, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report