Since the next article uses one of the plug-ins recommended in this article, I will share it here. Very practical! You’ll love it!
Before we start recommending IDEA bytecode to view artifacts, 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) that aim to use the same bytecode, and they all give the same results.
What is bytecode? What are the benefits of adopting bytecode?
In Java, code that can be understood by the JVM is called bytecodes (files with a.class extension) that are not oriented to any particular processor, just to the virtual machine. Java solves the problem of low efficiency of traditional interpreted languages by means of bytecode to some extent, and at the same time retains the portability of interpreted languages. So Java programs run more efficiently, and because bytecode is not specific to a particular machine, Java programs can run on many different operating systems without recompiling.
Java programs from source to run generally have the following three steps:
Why look at Java bytecode?
We often need to look at the bytecode file of a Java class in our daily study. Looking at bytecode files makes it easier to understand the philosophy behind Java code such as the nature of the various syntactic sugar in Java.
How do I view Java bytecode?
If we were to view the bytecode files of a class from the command line, we could use the Javap command directly, but this approach is primitive, inefficient, and unintuitive.
Here are two ways to view the bytecode files corresponding to a class using IDEA (not mentioned in 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); }}Copy the code
The above code is spooky with NPE exceptions due to improper use of the ternary operator. To understand why, let’s look at the bytecode.
Use the built-in function of IDEA
Click View -> Show Bytecode to View the Bytecode file corresponding to a class through IDEA.
One caveat: Make sure a class’s bytecode file has been compiled before you look at it.
After a few seconds, you can see the bytecode content of the corresponding class intuitively.
As you can see from the bytecode, the location I circled has been unpacked.
- Boxing: Wrapping base types with their corresponding reference types;
- Unpacking: convert packaging type to basic data type;
Flag? 0: I in this code, 0 is the basic data type int. When the data is returned, I will be forcibly unpacked as an int. Because the value of I is null, an NPE exception is thrown.
Integer i = null;
Boolean flag = false;
System.out.println(flag ? 0 : i);
Copy the code
If we change the flag variable to true in the code, there will be no NPE problem because 0 will be returned and no unpacking will take place.
Using IDEA plugin Jclasslib (recommended)
Jclasslib is a plugin that I recommend over IDEA to view class bytes.
usejclasslib
Not only can you visually view the bytecode files corresponding to a class, but you can also view the basic information of the class, constant pool, interface, properties, functions, and so on.
We can find this plugin directly in the plugin market of IDEA. I have it installed here.
After the installation, restart IDEA. View -> Show Bytecode With jclasslib to View the Bytecode file corresponding to a class.
My open source project recommendation
- JavaGuide: “Java Learning + Interview Guide” covers the core knowledge that most Java programmers need to master. Prepare for a Java interview, preferably JavaGuide!
- Guide xml-rpc – framework: Implemented by Netty+Kyro+Zookeeper A Custom RPC Framework Implemented by Netty+Kyro+Zookeeper
- Jsoncat: a lightweight HTTP framework that mimics but differs from Spring Boot
- Advancement: Good Habits for Programmers + Essentials for Interviews
- Springboot-guide: Not only SpringBoot but also important knowledge of Spring
- Awesome – Java: Collection of Awesome Java Project on Github
I have compiled a high-quality original PDF resource to share with you for free. Most of the content is my original, and a few are from friends.
Download address: cowtransfer.com/s/fbed14f0c… .