首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

种对象创建过程

2012-12-25 
类对象创建过程StringBuffer x new StringBuffer()执行过程为例:Java Virtual Machine Online Instruct

类对象创建过程

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.

热点排行