Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different behaviour of java bytecode

Tags:

java

bytecode

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.

like image 416
neel Avatar asked Aug 04 '12 20:08

neel


People also ask

How is Java bytecode is executed?

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.

What is bytecode manipulation in Java?

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.

What is the size of bytecode in Java?

Each can be independently sized from 0 to 65535 values, where each value is 32 bits.

What are bytecode instructions?

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.


1 Answers

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.

like image 170
Mark Byers Avatar answered Oct 19 '22 21:10

Mark Byers