I am a newbee in Java Bytecode. I was understanding the bytecode through some examples but I got stuck in an example.
These are my java and bytecode file
class SimpleAdd{
public static void main(char args[]){
int a,b,c,d;
a = 9;
b = 4;
c = 3;
d = a + b + c;
System.out.println(d);
}
}
Compiled from "SimpleAdd.java"
class SimpleAdd extends java.lang.Object{
SimpleAdd();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(char[]);
Code:
0: bipush 9
2: istore_1
3: iconst_4
4: istore_2
5: iconst_3
6: istore_3
7: iload_1
8: iload_2
9: iadd
10: iload_3
11: iadd
12: istore 4
14: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
17: iload 4
19: invokevirtual #3; //Method java/io/PrintStream.println:(I)V
22: return
}
I just want to know why there is bipush 9 when we have instruction a = 9
And in all other case there is iconst.
They can be executed by intepretation, just-in-time compiling, or any other technique that was chosen by the designer of a particular JVM. A method's bytecode stream is a sequence of instructions for the Java virtual machine. Each instruction consists of a one-byte opcode followed by zero or more operands.
Bytecode is the instruction set of the Java Virtual Machine (JVM), and all languages that run on the JVM must eventually compile down to bytecode. Bytecode is manipulated for a variety of reasons: Program analysis: find bugs in your application. examine code complexity.
Each can be independently sized from 0 to 65535 values, where each value is 32 bits.
Bytecode is program code that has been compiled from source code into low-level code designed for a software interpreter. It may be executed by a virtual machine (such as a JVM) or further compiled into machine code, which is recognized by the processor.
iconst
can push constant values -1 to 5. It is a single-byte instruction.
bipush
can push constant values between -128 and 127. It is a two-byte instruction.
To push 9 you cannot use iconst
. There is no iconst_9
instruction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With