Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

return

Example

A method that returns a value:

public class Main {

  static int myMethod(int x) {

    return 5 + x;

  }

  public static void main(String[] args) {

    System.out.println(myMethod(3));

  }

}

// Outputs 8 (5 + 3)

Definition and Usage

The “return” keyword terminates the execution of a method and can be utilized to send back a value from the method.

More Examples

Hint: Utilize the void keyword to indicate that a method does not return any value.

Example

A method that does not return any value:

public class Main {
 
static void myMethod() {
    System.out.println("I just got executed!");
  }

 
public static void main(String[] args) {
    myMethod();
 }
}