Java return statement

Introduction

The return statement will return from a method.

It transfers control back to the caller of the method.

The return statement can stop execution of current method and jump back to the caller of the method.

The following example illustrates the return statement.

// Demonstrate return.
public class Main {
  public static void main(String args[]) {
    boolean t = true;

    System.out.println("Before the return.");

    if(t) {/*from w w w . j  av  a2 s  . c  o  m*/
       return; // return to caller
    }

    System.out.println("This won't execute.");
  }
}

The final println() statement is not executed.




PreviousNext

Related