The return Statement


The return statement is used to explicitly return from a method. That is to say that it causes a program control to transfer back to the caller of the method.
ReturnDemo.java (the return statement)
// Java program to illustrate using return 
class ReturnDemo { 
  public static void main( String args[ ] ) { 
    boolean bool = Boolean.parseBoolean( args[0] ); 
    ReturnDemo myObj = new ReturnDemo( );
    if ( bool ) {
      System.out.print( myObj.myMethod( 10 ) );
      return;
    }
    System.out.print( 20 );
  }

  int myMethod( int n ) {
    return( n + 5 );
  }
}
shell> java ReturnDemo     ⇒   Output:    

      Result:

If a method is without any return values, use the void keyword to specify that a method should not have a return value:
VoidDemo.java (the void keyword)
// Java program to illustrate using void keyword
public class VoidDemo {
  public static void main( String args[ ] ) {
    myMethod( args[0] );
  }

  static void myMethod( String a ) {
    System.out.print( a );
    return( );
  }
}
shell> java VoidDemo     ⇒   Output:    

      Result:




      All roads lead to Rome.