Understanding bytecode makes you a programmer

Bytecode is the representation of Java just as assembler is the intermediate of C or C++ .

Java Bytecod is generated by the javac compile.

The bytecode is your program, regardless of a JIT or Hotspot runtime.

Java Bytecode is an important part of the size and execution speed of your code.


See A Simple Java Code:

public class person{
	private String name ;
	private int age ; 

	public void setName(String n){
		this.name = n ; 
	}

	public void SetAge(int a){
		this.age = a ;
	}

	public String getName(){
		return this.name;
	}

	public int getAge(){
		return this.age;
	}

	public String toString(){
		return this.name + "\t" + this.age ; 
	}
}


compilation .java to .class

javac person.java 

javap -c person > person.bc  
 
cat person.bc 

Compiled from "person.java"
public class person {
  public person();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        

  public void setName(java.lang.String);
    Code:
       0: aload_0       
       1: aload_1       
       2: putfield      #2                  // Field name:Ljava/lang/String;
       5: return        

  public void SetAge(int);
    Code:
       0: aload_0       
       1: iload_1       
       2: putfield      #3                  // Field age:I
       5: return        

  public java.lang.String getName();
    Code:
       0: aload_0       
       1: getfield      #2                  // Field name:Ljava/lang/String;
       4: areturn       

  public int getAge();
    Code:
       0: aload_0       
       1: getfield      #3                  // Field age:I
       4: ireturn       

  public java.lang.String toString();
    Code:
       0: new           #4                  // class java/lang/StringBuilder
       3: dup           
       4: invokespecial #5                  // Method java/lang/StringBuilder."<init>":()V
       7: aload_0       
       8: getfield      #2                  // Field name:Ljava/lang/String;
      11: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      14: ldc           #7                  // String \t
      16: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      19: aload_0       
      20: getfield      #3                  // Field age:I
      23: invokevirtual #8                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      26: invokevirtual #9                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      29: areturn       
}


A JVM is a stack-based machine. 

Each thread has a JVM stack which stores frames.

A frame is created each time a method is invoked, and consists of an operand stack, an array of local variables, and a reference to the runtime constant pool of the class of the current method. 

Understanding bytecode makes you a programmer

The array of local variables contains the parameters of the method and is also used to hold the values of the local variables. 

The parameters are stored first, beginning at index 0. 

If the frame is for a constructor or an instance method, the reference is stored at location 0. 

Then location 1 contains the first formal parameter, location 2 the second, and so on. 

For a static method, the first formal method parameter is stored in location 0, the second in location 1, and so on.

The size of the array of local variables is determined at compile time and is dependent on the number and size of local variables and formal method parameters. 

The operand stack is a LIFO stack used to push and pop values. 

Its size is also determined at compile time. 

Certain opcode instructions push values onto the operand stack; 

others take operands from the stack, manipulate them, and push the result. 

The operand stack is also used to receive return values from methods.



参考文献:

http://en.wikipedia.org/wiki/Java_bytecode

http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/

Java Plantform , Standard Edition (JavaSE 8) http://docs.oracle.com/javase/8/




















你可能感兴趣的:(java,jvm,bytecode)