1. Operation code for objects
The new operation code is used to instantiate a new object.
Object creation
Operation Code
Operands
Description
New
Index
Create a new object in the heap and push its reference to the stack.
The new operation code is followed by an unsigned 16-digit number, indicating an index in the constant pool. The constant pool entry at a specific offset location provides information about the class to which the new object belongs. If this information is not available, the constant pool entry will be parsed as a virtual opportunity. It creates a new instance for the object in the heap, initializes the object instance variables by default, and then pushes the reference of the new object to the stack.
Access instance variables
Operation Code
Operands
Description
Putfield
Index
Set the value of the object field (specified by index). Both the value and the object reference objectref are obtained from the stack.
Getfield
Index
Press the object field (specified by index) into the stack, and the object is obtained from referencing the objectref stack.
Access variables
Operation Code
Operands
Description
Putstatic
Index
Sets the value of a static field (specified by index). The value is obtained from the stack.
Getstatic
Index
Push static fields (specified by index) to stack
The putfield and getfield operation codes are only executed when the field is an instance variable. putstatic and getstatic perform access operations on static variables. The operand indicates the index of the constant pool. The constant pool entry pointed to by this index contains information about the class, name, and type of the field. If this information is not available, the constant pool entry will be resolved as a virtual opportunity.
For example, the following code:
Public class testa {
Int x;
Int y;
}
Public class testmain {
/**
* @ Param args
*/
Public static void main (string [] args ){
// Todo auto-generated method stub
Testa = new testa ();
Testa. x = 3;
Testa. y = 4;
}
}
Run the following command to view the bytecode of javap:
Compiled from "testmain. java"
Public class testmain extends java. lang. object {
Public testmain ();
Code:
0: aload_0
1: invokespecial #8; // method java/lang/object. "<init>" :() v
4: return
Public static void main (java. lang. string []);
Code:
0: new #16; // class testa new testa object
3: dup //
4: invokespecial #18; // method testa. "<init>" :() v calls the constructor.
7: astore_1 // local variable with the storage location of 1
8: aload_1 // push the local variable at 1 to the stack
9: iconst_3 // constant 3 into the stack
10: putfield #19; // field testa. x: I value assignment
13: aload_1
14: iconst_4
15: putfield #23; // field testa. y: I
18: return
}