Return Notes:-
o The return statement is used to explicitly return from a method.
That is, it causes program control to transfer back to the caller of
the method, hence a jump statement.
o Any time in a method the return statement can be used to cause
execution
to branch back to the caller of the method. Thus, the return statement
immediately terminates the method in which it is executed.
EXAMPLE:
The returnstatement here causes execution to return to the Java
run-time system,
since it is the run-time system that calls main( ).
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
The output from this program is shown here:
Before the return.
o The return statement is used to explicitly return from a method.
That is, it causes program control to transfer back to the caller of
the method, hence a jump statement.
o Any time in a method the return statement can be used to cause
execution
to branch back to the caller of the method. Thus, the return statement
immediately terminates the method in which it is executed.
EXAMPLE:
The returnstatement here causes execution to return to the Java
run-time system,
since it is the run-time system that calls main( ).
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
The output from this program is shown here:
Before the return.