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.)
|