×

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


Related Topics

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Java For Loop

A for loop is used to execute a set of statements for a fixed number of times. It takes the following form: for (initialization; condition; update) { statements; } The for loop defines...

2 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Java Switch Keyword

In this article we are going to learn the concept of a java switch keyword. Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in...

3 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Java Double Keyword

In java primitive data types, we have two different types of data types which are Boolean and floating-point data types.In floating data type again we have four types which are...

3 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.