Java के Return Values
In this page:
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
// 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));
}
}
Login to try C/C++/Java/PHP code in the editor
Void Return Type
void declared एक method कुछ भी return न करने का वादा करता है, लेकिन आप अब भी बिना value वाला एक bare return; लिखकर method जल्दी exit कर सकते हैं -- किसी condition के तहत execution बीच में रोकने के लिए उपयोगी।
उदाहरण: Void Return Type
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Returning Boolean Values
एक boolean-returning method (अक्सर isValid() या hasPermission() जैसा नाम दिया) एक if condition के अंदर naturally पढ़ता है और एक yes/no check के लिए standard shape है जिसे आप कहीं और reuse करने की योजना बना रहे हों।
उदाहरण: Returning Boolean Values
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Returning Object References
एक object या array return करने का मतलब है caller को उसी data का एक reference मिलता है, एक पूरी copy नहीं -- और जब return type एक array है, declaration square brackets उपयोग करती है, जैसे int[] getScores()।
उदाहरण: Returning Object References
// 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]);
}
}
Login to try C/C++/Java/PHP code in the editor
Return Statements in Loops
एक loop के अंदर एक return statement एक step में loop और पूरे enclosing method से exit होता है, method के ज़्यादा code चलाए बिना तुरंत इसकी value caller को वापस सौंपते हुए।
उदाहरण: Return Statements in Loops
// 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}));
}
}
Login to try C/C++/Java/PHP code in the editor
- एक non-void method में हर path पर एक value
returnकरना भूल जाना, जोmissing return statementcompile error है। - एक
voidmethod से एक value return करना, जो एक compile error है। - Returned value नज़रअंदाज़ करना, इसलिए एक computed result खो जाता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: