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

How does the Java code execute

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how the Java code is executed". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how the Java code is executed.

1. Compiled into class

As we all know, java code cannot be executed directly on jvm. It executes class files. Java code is programmed into class files and needs to be compiled.

The commonly used compilation method is: javac xxx.java

But at present, common java editing tools, such as eclipse and ideal, have their own automatic compilation kinetic energy.

2. Composition of jvm

The theme is divided into five parts:

Method area, local method stack, java stack, java stack, program counter

Among them, the java stack, the local method stack, the program counter are private to threads, and the rest are shared by threads.

So, where is the method executed?

Java stack.

The stack is followed by first in and then out, and the execution of the method in the java stack also follows this rule, and the steps of the method execution are also known as stack frames.

3. Sequential execution and stack frames of methods

The above code:

Public class Main {public static void a () {b ();} public static void b () {c ();} public static void c () {System.out.println ("Hello world!");} public static void main (String [] args) {a ();}}

The above is a very simple piece of code, the main body is:

(1) A Main class

(2) A main method is defined above

(3) the main method calls the static method a

(4) method a calls method b

(5) method b calls method c

(6) method c prints "Hello world!"

As mentioned earlier, the non-local methods defined by java are executed in the java stack, one method per stack.

So let's assume

Mian method corresponds to stack frame m

A method corresponds to stack frame ab method corresponds to stack frame bc method corresponds to stack frame c

According to the invocation of the method, the order of entering the stack is as follows: mmae _ r _ a _ m _ b _ c

Therefore, the order in which stack frames are unstacked (that is, method execution) is as follows: crecoery brecoery arecom.

4. What the class file looks like after decompilation

In the previous section, the order in which the method or stack frames are executed on the java stack, but how the content in the method body is executed.

As mentioned earlier, jvm executes a class file, but what is in the class file?

Inside the class file is a set of instructions.

How to prove it, or look at another piece of code.

Public class Calculator {public int add () {int n = 10; int m = 20; int r = n + m; return r;} public static void main (String [] args) {Calculator calculator = new Calculator (); int a = calculator.add (); System.out.println (a);}}

As shown in the above code, the functions implemented are:

(1) define two variables and add them together.

(2) main method new object, call method

However, class files cannot be viewed directly.

We can use decompilation, decompilation commands:

Javap-c xxx.class

The Code under each method is a set of instructions.

5. Instruction set detail

Before we discuss the instruction set, we should first talk about the concept of further splitting the stack frame.

The stack frame is divided into four parts: local variable table, Operand stack, dynamic link, method return address.

Among them, the local variable table and the Operand stack are the two most important parts. The local variable table is stored in the local variables defined in the method. The Operand stack is equivalent to a cache of jvm. All operations must be carried out here, and all variables must be loaded into the Operand stack before they can be used. Therefore, the so-called instruction is the process of tossing back and forth between the local variable table and the Operand stack.

The following instructions are classified and explained:

(1) Stack instruction

Integer stack instruction:

Iconst instruction is used for the value-1 to 5.

The value-128 to 127 uses the bipush instruction

Value-32768mm 32767 uses sipush instruction

The value-21474836481147483647 uses the ldc instruction.

Non-integer stack instruction:

The float,String type also uses the ldc instruction

Double and long types use ldc_2w

The boolean type is treated as 0 and 1

The stack instruction of null is: aconst_null

(2) Storage instruction

Save the constant in the Operand stack somewhere in the local variable table

Such as:

Istore_1: save the integer constant on the stack above in the first place in the local variable table

Fstore_2: store the floating point constant on the stack above in the second position in the local variable table

Dstore_10: save the above stacked double floating point constant in the 10th position in the local variable table

Lstore_20: save the long integer constant above the stack in the 20th position in the local variable table

Astore_100: save the reference constant of the above stack in the 100th position in the local variable table

(3) variable stack instruction

Iload_1: integer variables in the first position in the local variable table are stacked

Fload_2: floating-point variables at the first position in the local variable table

Dload_10: double floating-point variables in the first position in the local variable table

Lload_20: the long integer variable of the first position in the local variable table is stacked

Aload_100: the referenced variable of the 100th position in the local variable table is stacked

(4) calculation instruction

Plus: iadd, ladd, fadd, dadd

Minus: isub, lsub, fsub, dsub

Multiply: imul, lmul, fmul, dmul

Except: idiv, ldiv, fdiv, ddiv

Note: when calculating at the top of the stack, only one expression can be evaluated at a time

Thank you for your reading, the above is the content of "how the Java code is executed". After the study of this article, I believe you have a deeper understanding of how the Java code is executed, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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