Jump statement using return in Java

In this tutorial, we will learn jump statement using return in Java.

using  jump statement (return) in java

We are showing you a simple example of it, so that you can understand how it works.

The return statement will explicitly return from a method.

Here, return 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.

As you can see, the final println() statement is not executed. as soon as the return is executed, control passes back to the caller.
One last point: in the preceding program, the if(t) statement is necessary. without it, the java compiler would flag an “unreachable code” error because the compiler would know that the last println() statement would never be executed. To prevent this error, the if statement is used here to trick the compiler for the sake of this demonstration.

You may also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *