Recently, in a systematic review of the knowledge of Java virtual machines, there is an interesting question: What is the type of Boolean in virtual machines?
To answer this question, we look at the JDK source code is not able to solve the source code, we must go deep into the class file to solve the problem. So he gave the question:
public class Foo{ static boolean flag; public static void main(String[] args){ flag = true; if(flag){ System.out.println("Hello, Java!"); } if(flag == true){ System.out.println("Hello, JVM!"); } }}
This problem is very simple, the result is output:
Hello, Java!Hello, JVM!
But we're going to go deep into the class file and see how the JVM executes bytecode instructions for this code. So we use the Javac command to get its class bytecode file:
javac Foo.java
Bytecode files are hexadecimal character set, we can generally use the JAVAP command to achieve disassembly. But this time we use another tool, namely asmtools. It is another disassembly tool provided by OpenJDK.
java -cp /path/to/asmtools.jar org.openjdk.asmtools.jdis.Main Foo.class > Foo.jasm.1
Note: The above need to download Asmtools this Jar package,: Asmtools-code tools-openjdk Wiki
This asmtools jar package is not very easy to find, I am here to talk about where to download. After opening the link above you can see the following diagram:
Click Release in the Red box:
Then click to download the asmtools*.tar.gz file, after decompression can see the corresponding asmtools file.
This line of command is actually to disassemble the bytecode file, and then exist in the Foo.jasm.1 file. We open the foo.jasm.1 file to see what follows:
In fact, this file is a series of byte code instruction set, the above main method of the bytecode instruction we can be divided into two parts.
The first part of the bytecode directive is this:
Click on the image to see a detailed explanation of each bytecode instruction
In fact, the logic of these lines corresponds to the following source code:
if(flag){ System.out.println("Hello, Java!");}
And the second part of the byte Code instruction analysis:
Click on the image to see a detailed explanation of each bytecode instruction
The logic of these lines corresponds to the following source code:
if(flag == true){ System.out.println("Hello, JVM!");}
After reading these two parts of the bytecode directive, you will find only iload_1 and iconst_1 bytecode instructions, and these two bytecode instructions are handling int type data. So we can see that the Boolean type in the JVM is stored with the int type.
In fact, there is a way to visually see that the Boolean type is represented by the integer type, that is, the Foo.jasm file is restored to a class file. Run the following command to restore it to a class file:
java -cp asmtools.jar org.openjdk.asmtools.jasm.Main Foo.jasm
You will find that a Foo.class file is generated under the directory, and we use the Jd-gui tool to open it:
You will find that the Boolean variant is now an integral type. The original true is now 1.
To summarize, in fact, the Boolean type in the Java virtual machine is represented by an integral type, true with 1, false with 0 represents.