×

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

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 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.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Java Set to List

In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process. First...

4 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.