257
Java return Keyword
Java return keyword is used to complete the execution of a method. The return followed by the appropriate value that is returned to the caller. This value depends on the method return type like int method always return an integer value.
Points to remember
- It is used to exit from the method.
- It is not allowed to use return keyword in void method.
- The value passed with return keyword must match with return type of the method.
Examples of Java return Keyword
Example 1
Let’s see a simple example to return integer value.
Output:
10
Example 2
Let’s see an example to determine whether we can use return in void method.
Output:
Void methods cannot return a value
Example 3
Let’s see an example to return string.
Output:
TutorAspire
Example 4
Let’s see an example to return list of elements.
Output:
[Java, C++, Python]
Next TopicJava Keywords