×

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

How to add 24 Hours to Date in Java?

In this tutorial, we will learn how to add 24 hours to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Binary Strings Without Consecutive Ones in Java

N, an integer, is provided. Our objective is to determine the overall number of strings with length N that do not include successive 1s. Example: Input: 3 Output: 6 Explanation The binary forms of length...

6 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.