return StatementThe return statement in Java is used to send a value back from a method to the caller and immediately terminate the method execution.
void)A method with a return value must use the return keyword followed by a value of the correct type.
// Method returning an integer value
int add(int a, int b) {
return a + b;
}
// Complete Java program demonstrating return statement
class ReturnDemo {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
int result = square(5);
System.out.println(result);
}
}
25
The method square() returns the square of the number passed to it, which is then printed in the main method.
return to simplify complex logictrue or false based on a conditionvoid method into one that returns a value