Return values let a method send back a result after it finishes. This helps you reuse results in your program.
Return values in Java
returnType methodName(parameters) {
// code
return value;
}The returnType is the type of value the method sends back, like int or String.
The return keyword sends the value back and ends the method.
int.int add(int a, int b) { return a + b; }
true or false.boolean isEven(int number) { return number % 2 == 0; }
String greet(String name) { return "Hello, " + name + "!"; }
This program defines a method multiply that returns the product of two numbers. The main method calls it and prints the result.
public class ReturnExample { public static int multiply(int x, int y) { return x * y; } public static void main(String[] args) { int result = multiply(4, 5); System.out.println("4 times 5 is " + result); } }
Every method with a return type other than void must return a value of that type.
Once return runs, the method stops immediately.
Methods with void do not return a value and cannot use return with a value.
Return values let methods send back results to use elsewhere.
Use the return keyword followed by the value to send it back.
The method's return type must match the type of the returned value.
Practice
return keyword do in a Java method?Solution
Step 1: Understand the purpose of
Thereturnreturnkeyword is used inside a method to send a value back to the caller.Step 2: Differentiate from other actions
Stopping the program, printing, or declaring variables are different actions and not whatreturndoes.Final Answer:
It sends a value back from the method to where it was called. -> Option BQuick Check:
Return sends value back [OK]
- Thinking return stops the whole program
- Confusing return with print statements
- Assuming return declares variables
Solution
Step 1: Check method return type and return statement
The method must declareintas return type to return an integer value.Step 2: Match return type with returned value
public int getNumber() { return 5; } declaresintand returns 5, which is an integer, so it is correct.Final Answer:
public int getNumber() { return 5; } -> Option CQuick Check:
Return type matches returned value [OK]
- Using void but returning a value
- Returning wrong type value
- Missing return statement in non-void method
public class Test {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(3, 4));
}
}Solution
Step 1: Understand the add method
The method adds two integers and returns their sum.Step 2: Calculate add(3, 4)
3 + 4 equals 7, so the method returns 7.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings
- Expecting error due to return
- Confusing return with print
public int multiply(int x, int y) {
int result = x * y;
}Solution
Step 1: Check method return type
The method declares it returns an int, so it must return an int value.Step 2: Verify return statement presence
The method calculates result but does not return it, so it misses a return statement.Final Answer:
Method is missing a return statement. -> Option DQuick Check:
Non-void methods must return a value [OK]
- Forgetting to return value in non-void method
- Changing return type to void without reason
- Assuming variable declaration is error
public static String checkNumber(int n) {
if (n > 0) return "Positive";
else if (n < 0) return "Negative";
else return "Zero";
}What will
checkNumber(0) return?Solution
Step 1: Analyze the if-else conditions
If n > 0 returns "Positive", if n < 0 returns "Negative".Step 2: Check the else case for n = 0
Since 0 is neither greater nor less than 0, else returns "Zero".Final Answer:
"Zero" -> Option AQuick Check:
0 triggers else return "Zero" [OK]
- Assuming 0 is positive or negative
- Missing else return case
- Expecting null when no return
Ready to prove it?
Pass the test and this pattern moves unknown
