Home >Java >javaTutorial >How to use javap to parse Java integer constants and integer variables
The content of this article is about how to use javap to parse Java integer constants and integer variables. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The fifth and ninth lines of my code below define an integer variable and an integer constant respectively:
static final int number1 = 512; static int number3 = 545;
Java programmers all know the difference between the two.
Next we will use javap to decompile the .class file and then delve into the difference between integer variables and integer constants in Java.
Use the command line javap -c constant.ConstantFolding to view the bytecode decompiled from the .class file:
Result:
The description of these bytecode instructions is explained in wikipedia:
wiki: https://en.wikipedia .org/wiki...
We Java programmers don’t need to memorize them all. We only need to save this web page and use it as a dictionary when we need it:
sipush 545: Place the integer 545 on the stack
putstatic #16:
Assign the value 545 on the stack to the current class in the static field.
So what does #16 in putstatic #16 mean?
If we use javap -v parameter to decompile, we can see the constant pool of this class (Constant pool). Look at the blue highlighted line in the picture below:
constant/ConstantFolding .number3:I
Description #16 represents member number3 of class constant.ConstantFolding, type is I.
At this point, these two lines of bytecode instructions are combined and actually correspond to the Java code we wrote:
static int number3 = 545;
We Continue to analyze the bytecode decompiled by javap.
aload_0: Load the introduction of local variables with serial number 0 onto the stack
invokespecial: Invoke members on the object instance Method, if there is a return value, the return value of the method is stored on the stack. The specific method called is identified by #, and the corresponding method name can be queried in the constant pool.
ldc: Load the value of the constant codenamed #
We can find from the constant pool list in the figure below that the constant 318976 with serial number #29 is exactly the product of the integer constant number1 (512) and the integer constant (623). It can be seen from this that the expression number1 * number2, because the two operands involved in the operation have become integer constants through STATIC and FINAL modifications, the product can be obtained at compile time, so the compiler calculates it at compile time Come out and store it in the variable pool with the sequence number #29.
So what is the corresponding bytecode for multiplication of integer variables?
Start from code number 3 in the figure below:
getstatic #16: Load the static member #16 of the class onto the stack. The corresponding member of #16 is number3 and the value is 545.
getstatic #18: Load the static member #18 of the class onto the stack. The corresponding member of #18 is number4 and the value is 619.
imul: Perform multiplication of two integers on the stack.
istore_2: Save the result to local variable 2.
At this point, the int product2 = number3 * number4 in our Java code has been executed.
The remaining blue bytecode you see corresponds to the following line of print statements.
System.out.println("Value: " + product1 + " , " + product2);
It can also be seen from these bytecodes that in Java we directly use the plus sign to perform string splicing operations. The Java compiler automatically uses StringBuilder for optimization when compiling.
Since the product of integer variables needs to be printed out, iload_2 of the bytecode loads the calculation result previously saved in local variable 2 with istore_2 onto the stack, so that the product result can be output at the end.
#I hope that through this simple example, everyone can learn to use javap to deeply understand some details of Java and JVM.
The above is the detailed content of How to use javap to parse Java integer constants and integer variables. For more information, please follow other related articles on the PHP Chinese website!