类对象创建过程

StringBuffer x = new StringBuffer();执行过程为例:

Java Virtual Machine Online Instruction Reference 写道
; This example creates a new StringBuffer object. This is like the Java code:
;
; StringBuffer x = new StringBuffer();

; 1. use new to create a new object reference
new java/lang/StringBuffer

; 2. dup the object reference and call its constructor
dup
invokespecial java/lang/StringBuffer/<init>()V

; 3. assign object reference on the stack to a local variable
astore_1
; local variable 1 now contains a StringBuffer object,
; ready for use

 

 

为StringBuffer类尚未初始化对象分配内存空间,并将对象引用压入操作数栈.
复制操作数栈顶数据,这里即是复制了操作数栈顶对象引用.
栈顶对象引用出栈,调用对象所属类构造函数.
当前栈顶元素出栈,存入局部变量数组.

特别地,如果只是new StringBuffer()创建一个临时对象时,astore_1则会变更成pop.

你可能感兴趣的:(java)