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 learn about return statement in Java which is used to explicitly return from a method.
Java return statement
When a return statement is encountered in a method that method’s execution is terminated and control transfers to the calling method. A return statement may or may not return a value to the calling method.
Rules for using Java return statement
- If no value is returned from the method then the method signature should signify it by having void in method signature. For example- void methodA()
- If a method is not returning any value i.e. its a void method then having return statement in that method is not mandatory. Though an empty return can still be there which can be used as a control statement to terminate execution of the method for given condition.
- If a method is returning a value then return type should be signified in the method signature. For example a method that returns int should have it in its method signature- int methodA()
- The return type of the method and actual value returned should be compatible.
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