In previous tutorials we have already seen continue statement which is used to continue the iteration of a loop and break statement which is used to break out of a loop, in this post, we’ll explore the return statement in Java, a fundamental control flow element which is used to explicitly return from a method.
What is the Return Statement in Java
When a return statement is encountered in a method that method’s execution immediately terminates and control transfers back to the calling method. Depending on the method’s signature, a return statement may return a value or simply exit without returning anything.
Rules for using Java return statement
- Void methods and return
- If a method does not return a value, its signature must declare void.
- Example: void methodA() { ... }
- In such methods, a return; statement is optional and can be used to exit early under certain conditions.
- Methods returning a value
- If a method returns a value, its signature must specify the return type.
- Example: int methodB() { return 5; }
- The type of the returned value must be compatible with the declared return type.
- Type compatibility
- The return type of the method and actual value returned should be compatible.
- The compiler enforces strict type checking. For instance, a method declared to return double cannot return a String.
Java return keyword example
1- A method returning int value.
public class ReturnExample {
public static void main(String[] args) {
ReturnExample obj = new ReturnExample();
int sum = obj.add(6, 7);
System.out.println("Sum is- " + sum);
}
int add(int a, int b) {
int sum = a + b;
return sum;
}
}
Output
Sum is- 13
2- A void method with return statement as a control statement to terminate method execution when the condition is satisfied.
public class ReturnExample {
public static void main(String[] args) {
ReturnExample obj = new ReturnExample();
obj.display();
System.out.println("After method call...");
}
void display() {
for(int i = 1; i <= 10; i++) {
// method execution terminates when this
//condition is true
if (i > 5)
return;
System.out.println(i);
}
}
}
Output
1 2 3 4 5 After method call...
In the example, there is a for loop in method with a condition to return from the method. When the condition is true, method execution is terminated and the control returns to the calling method.
One point to note here is that in a method return statement should be the last statement or it should be with in a condition. Since method execution terminates as soon as return statement is encountered so having any statements after return results in “Unreachable code” error.
public class ReturnExample {
public static void main(String[] args) {
ReturnExample obj = new ReturnExample();
obj.display();
System.out.println("After method call...");
}
void display() {
int i;
return;
i++; // error
}
}
In the example there is code after the return statement which is never going to be executed thus the “Unreachable code” compile time error.
That's all for this topic return Statement in Java With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Basics Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment