Java Return Keyword
The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as int, determines this value because it always returns an integer value.
When a method's execution is finished, the return keyword is used to exit the method. When a method's return statement is reached, the application goes back to the code that called it.A method may return a value, a reference type, or it may not. A method must be marked void and need not have a return statement if it does not return a value.
If a method declares that it will return a value, the return statement must be used in the method body. The method's return type and the return value's data type must be compatible. As a result, methods specify the type of the return value. Consequently, the type of the return value is specified for methods.
The actual returning in Java is done via the return keyword. Return is not an identifier in Java because it is a reserved term. It's employed to end a method, either with or without a value. There are two ways to use the return keyword, which are listed below:
Case 1: Procedures that return values
Case 2: Methods that fail to return any values
Important Points to remember:
- It is utilized to leave the procedure.
- The return keyword cannot be used in void methods.
- The value given with the return keyword must be compatible with the method's return type.
Examples of Return Keyword
Example 1:
An easy example for returning an integer value.
public class ReturnEx1 {
int disp()
{
return 40;
}
public static void main (String [] s) {
ReturnEx1 e =new ReturnEx1();
System.out.println(e. disp());
}
}
Output:
40
Example 2:
An illustration to show whether return can be used in void methods.
public class ReturnEx2 {
void disp ()
{
return null;
}
public static void main (String [] s) {
ReturnEx2 e =new ReturnEx2();
e. disp();
}
}
Output:
Void methods cannot return a value
Example 3:
view a string return sample.
public class ReturnEx3 {
String disp()
{
return "Java lang";
}
public static void main (String [] s) {
ReturnEx3 e =new ReturnEx3();
System.out.println(e. disp ());
}
}
</pre></div>
<p><strong>Output:</strong></p>
<div class="codeblock3"><pre>
Java lang
</pre></div>
<h2 class="h3">Example 4</h2>
<p>Let's see an example to return list of elements. </p>
<div class="codeblock"><textarea name="code" class="java">
import java.util.*;
public class ReturnEx4 {
List disp()
{
List list=new ArrList();
list.add("C");
list.add("C++");
list.add ("Java script");
return list;
}
public static void main (String [] s) {
ReturnEx4 e =new ReturnEx4();
System.out.println(e. disp());
}
}
Output:
[C, C++, Java script]
Example 4:
A method can take outside input and return the results. A return statement is used inside of a method to exit it and return the result to the caller method. The Java language's return keyword is used to end a method with or without returning a value. In Java, a return type must be defined for every method, and it is required for all Java methods.
A primitive data type, such as int, float, or double, a reference type, or void type, which stands for "return nothing," may be used as the return type. In other words, they don't return anything. When a method is called, the called method could receive a value in return.
Let's look at a sample program to get the idea
public class Te
{
int sq (int n) {
return n * n; // return a square value.
}
public static void main (String [] s)
{
// Create an obejct of class Test.
Te n = new Te ();
int squareOfNum = n. sq(10);
// Displaying the result.
System.out.println("Square of 20: " +squareOfNum);
}
}
Output:
Square of 10: 100
As we can see, this is the program's entry point, and the main method is being executed. Code is executed line by line, starting at the top. No value may be returned by our main method. As a result, the procedure only prints to the screen. Let's now insert the string literal into a different message-generating method