Last Updated: 28 May, 2022
In Java, 'return' is a keyword that is used to return a value after the execution of a method. After executing the return statement, the execution control returns to the code that invoked it.
A method can return a single value, a group of values, an object, or a group of objects. If a method does not return a value, then that method should be declared void.
If a method returns a value, then it must use the return statement within the body of the method. The data type of the return value must match the method’s declared return type.
Syntax of return statement:
return value;
Let's understand the use of return statements in Java programs in different scenarios.
Example 1 : 'return' Statement in Java
Output
Total Marks: 275
Returning a String value.
Example 2 : Multiple return statements in a method
We can write multiple return statements in a method, but they must be based on conditions. Let's see the example below.
Output
TEN
HUNDRED
Not Matched
Example 3 : Method not returning any value
Output
printMessage() method called.
Example 4 :
Output
Course Name:
JAVA
Python
C
C++
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com