3.5 Returns

When a method is finished executing, it must return control to its caller. The caller is often expecting a value from the called method. For this purpose, the JVM has six return instructions, as summarized in Table 3.2. After one of the xreturn statements has been executed, control transfers to the instruction immediately after whatever invoke instruction began the method. The calling method sees this as though the invoke instruction were like any other instruction that had just completed, and it moves on to the next instruction after the invoke. Whatever value was on the stack when the xreturn instruction was executed is now on the top of the stack in the method returned to. (If the instruction was a return instruction, then there is no value.) The arguments to the method are removed.

The value returned by the xreturn statement must agree with the type of the method. If the return type is V for void, then only return can be used. If the return type is I for int, only ireturn can be used. If the return type is a reference, then the areturn opcode must be used, and you must be able to cast the returned value to the type specified in the method descriptor. (See section 4.6 for more about casting.)

Table 3.2. Return instructions
Mnemonic Stack Effect
areturn reference Control returns to caller with reference on the stack.
ireturn int Control returns to caller with int on the stack.
freturn float Control returns to caller with float on the stack.
dreturn double Control returns to caller with double on the stack.
lreturn long Control returns to caller with long on the stack.
return   Control returns to caller.

Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net