dup
duplicate top single-word item on the stack
1.Stack
| Before | After |
|---|---|
| item | item |
| … | item |
| … |
2.Description
This pops the top single-word value off the operand stack, and then pushes that value twice - i.e. it makes an extra copy of the top item on the stack.
This instruction cannot be used to duplicate two-word items (longs or doubles) - use dup2 instead.
3.Example
; This is like the java expression:
;
; StringBuffer x = new StringBuffer();
;
; Make a new StringBuffer object and leave a reference to it
; on the stack:
new java/lang/StringBuffer
; [ Stack now contains: objectref ]
; Duplicate the object reference:
dup
; [ Stack now contains: objectref objectref ]
; Invoke the object's initializer:
invokespecial java/lang/StringBuffer/<init>()V
; [ Stack now contains: objectref ]
; Store the objectref in local variable 1.
astore_1
; [ Stack is now empty. ]
本文详细介绍了Java字节码指令dup的功能和用法,该指令用于复制操作数栈顶的单字项,并将其再次压入栈中。通过一个具体的例子展示了如何使用dup指令来创建对象引用的副本。
1469

被折叠的 条评论
为什么被折叠?



