← Back to Core Java Course | Chapter 5: Methods & Arrays | Lesson 3 of 10

Java Return Values

A return value is the answer a method gives back when it is done, like a vending machine handing you a snack. A void method just does its job and gives nothing back.
Syntax
java
static returnType methodName(dataType parameter) {
    // body
    return value;
}

The return Keyword

The return keyword sends a value back to whatever code called the method, and that value's type must match (or be convertible to) the return type declared in the method's signature.

Example: The return Keyword

java
// Define the class Main
public class Main {
	static int square(int n) {
		// Send n * n back to the caller
		return n * n;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(square(5));
	}
}

Void Return Type

A method declared void promises to return nothing, but you can still write a bare return; with no value to exit the method early -- useful for stopping execution partway through under some condition.

Example: Void Return Type

java
// Define the class Main
public class Main {
	static void checkAge(int age) {
		// Check whether age < 0
		if (age < 0) {
			return;
		}
		// Print a line to the console
		System.out.println("Age: " + age);
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		checkAge(-1);
		checkAge(25);
	}
}

Returning Boolean Values

A boolean-returning method (often named like isValid() or hasPermission()) reads naturally inside an if condition and is the standard shape for a yes/no check you plan to reuse elsewhere.

Example: Returning Boolean Values

java
// Define the class Main
public class Main {
	static boolean isEven(int n) {
		// Send n % 2 == 0 back to the caller
		return n % 2 == 0;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Check whether isEven(4)
		if (isEven(4)) {
			// Print a line to the console
			System.out.println("Even");
		}
	}
}

Returning Object References

Returning an object or array means the caller receives a reference to that same data, not a full copy -- and when the return type is an array, the declaration uses square brackets, like int[] getScores().

Example: Returning Object References

java
// Define the class Main
public class Main {
	static int[] getScores() {
		// Send new int[] {90, 85, 77} back to the caller
		return new int[] {90, 85, 77};
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare scores and set it to getScores()
		int[] scores = getScores();
		// Print a line to the console
		System.out.println(scores[0]);
	}
}

Return Statements in Loops

A return statement inside a loop exits the loop and the entire enclosing method in one step, immediately handing its value back to the caller without running any more of the method's code.

Example: Return Statements in Loops

java
// Define the class Main
public class Main {
	static int findFirstEven(int[] nums) {
		for (int n : nums) {
			// Check whether n % 2 == 0
			if (n % 2 == 0) {
				// Send n back to the caller
				return n;
			}
		}
		// Send -1 back to the caller
		return -1;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(findFirstEven(new int[] {1, 3, 4, 5}));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to return a value on every path in a non-void method, which is a compile error missing return statement.
  2. Returning a value from a void method, which is a compile error.
  3. Ignoring the returned value, so a computed result is lost.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.