Introduction to the
Byte Code, also known as bytecode, is a bridge between Java source Code and the JVM. The source Code is compiled into bytecode, which is loaded into the JVM to run. How to generate bytecodes, how to view bytecodes, and what is the secret behind Byte Code? Come and have a look with my sister.
Function of Byte Code
Brother F, why does Java need bytecode? Isn’t it faster to compile directly to machine code?
Java was designed to be written once and run anywhere. Java has designed JVMS specifically for each platform in order to be compatible with each platform’s operating environment.
We can think of the JVM as an abstraction that provides a unified interface to the outside world. This way we only need to write code that conforms to the JVM specification to run in the JVM.
Recall the Java implementation we mentioned earlier:
- Write Java code files such as example.java
- Compile the source file into example.class using the Java compiler javac
- The JVM loads the generated bytecode files and converts them into native machine code that the machine can recognize for execution
Brother F, I have a bold idea that the purpose of JVM is to interpret or compile bytecode into machine code. It is then executed in the appropriate runtime environment. So is it possible, without the JVM, without the machine code, to execute bytecode directly on the corresponding platform?
Einstein said that a soul without imagination is like an observatory without a telescope. Your idea is very good. There is a professional term for this implementation: Java Processor.
Java Processor is a JVM implemented with hardware. So bytecode can run directly in the Java Processor.
One of the better known is Jazelle DBX, a hardware architecture that primarily supports a J2ME environment. To speed up Java execution on mobile phones.
But there’s a downside to this, as we’ll see later, because Java bytecode is very, very full of instructions. So if you do it in hardware, it’s very, very complicated.
In general, Java Processor does not implement all of the functionality in bytecode, but only a partial implementation.
View Byte Code
F: What does the class file compiled using Javac have to do with bytecode?
Most of the class file is byte code, and the rest is meta data metadata information. Together, these are the class files.
F: You said the class file is byte code. Why do I display the decompiled source file when I open it in the IDE?
Xiao Shi Mei, this is a convenient function of IDE. Because most of the time, nobody wants to look at the Byte code of the class file, everybody wants to look at the source of the class file.
Let’s take the simplest example:
In this class, we define a very simple testByteCode method that defines two variables and returns the sum of them.
There are now two ways to view the class’s Byte Code:
The first method is to use the javap command:
javap -c ByteCodeUsage.class
Copy the code
The resulting results are shown above.
The second method is to select the class file in IDEA and select Show Bytecode in view:
Let’s look at the output:
The two results may be slightly different in the display, but it does not affect our subsequent parsing.
How does Java Byte Code work
F, can you explain how these Byte codes work?
Let’s start by explaining that the JVM implementation is based on a stack structure. Why stack based structure? That’s because stacks are best used to make function calls to each other.
Let’s revisit the bytecode of testByteCode above. There’s a lot of iconst istore stuff in there, and these things are called Opcode, which is just some stack based operation instructions.
There are many Java Bytecode operation instructions. Here is a partial introduction to these instructions:
There are so many of them that I won’t list them all here.
The instruction name we see is actually a mnemonic; the actual Opcode is a two-byte number.
TestByteCode: testByteCode
public int testByteCode(a);
Code:
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: ireturn
Copy the code
First, iconst_1 loads int 1 into the stack.
In the second step, istore_1 pushes int 1 off the stack and stores it in variable 1.
In the third step, iconst_2 pushes int 2.
In step 4, istore_2 pushes int 2 off the stack and stores it in variable 2.
Fifth, ILoAD_1 pushes the value of variable 1 onto the stack.
In step 6, ILoAD_2 pushes the value in variable 2.
In step 7, iadd pushes the two variables off the stack and adds them. The result is then pushed onto the stack.
In step 8, iReturn pushes the result out of the stack.
These steps actually perfectly restore the functionality we defined in the testByteCode method.
Of course, we’ve only covered the meanest Byte Code commands, and these simple commands can be combined into more complex Java commands.
conclusion
This article introduces the functions of Java Byte code and specific instructions, and analyzes a simple example to illustrate. I hope you get the hang of it.
Examples of this article github.com/ddean2009/l…
Author: Flydean program stuff
Link to this article: www.flydean.com/jvm-byte-co…
Source: Flydean’s blog
Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!