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

Java के Return Values

एक return value वह answer है जो एक method खत्म होने पर वापस देता है, एक vending machine के आपको एक snack देने जैसा। एक void method बस अपना काम करता है और कुछ वापस नहीं देता।
Syntax
java
static returnType methodName(dataType parameter) {
    // body
    return value;
}

The return Keyword

return keyword एक value method को call करने वाले code को वापस भेजता है, और उस value का type method के signature में declared return type से match होना चाहिए (या इसमें convertible होना चाहिए)।

उदाहरण: 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

void declared एक method कुछ भी return न करने का वादा करता है, लेकिन आप अब भी बिना value वाला एक bare return; लिखकर method जल्दी exit कर सकते हैं -- किसी condition के तहत execution बीच में रोकने के लिए उपयोगी।

उदाहरण: 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

एक boolean-returning method (अक्सर isValid() या hasPermission() जैसा नाम दिया) एक if condition के अंदर naturally पढ़ता है और एक yes/no check के लिए standard shape है जिसे आप कहीं और reuse करने की योजना बना रहे हों।

उदाहरण: 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

एक object या array return करने का मतलब है caller को उसी data का एक reference मिलता है, एक पूरी copy नहीं -- और जब return type एक array है, declaration square brackets उपयोग करती है, जैसे int[] getScores()।

उदाहरण: 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

एक loop के अंदर एक return statement एक step में loop और पूरे enclosing method से exit होता है, method के ज़्यादा code चलाए बिना तुरंत इसकी value caller को वापस सौंपते हुए।

उदाहरण: 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. #}
आम गलतियां
  1. एक non-void method में हर path पर एक value return करना भूल जाना, जो missing return statement compile error है।
  2. एक void method से एक value return करना, जो एक compile error है।
  3. Returned value नज़रअंदाज़ करना, इसलिए एक computed result खो जाता है।

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.