Control Structures
5. Branching Structures
5.2. Return Statement
Exits a method and optionally returns a value to the caller.
Example:
public class ReturnExample {
public static int add(int a, int b) {
return a + b; // Exits method and returns sum
}
public static void main(String[] args) {
int result = add(5, 3);
System.out.println("Sum: " + result); // Output: Sum: 8
}
}
Notes:
Used to terminate a method’s execution.
If the method’s return type is not void, a value must be returned.