AppDividend
Latest Code Tutorials

Java Return Keyword Tutorial | Return Keyword in Java Example

0

Java Return Keyword Tutorial | Return Keyword in Java Example is today’s topic. The return keyword in Java and most programming languages are used to return (i.e., exit/terminate while returning/not returning a value) from a method called from one line of a program. See the following diagram.

 

Java Return Keyword Tutorial

In other words, calling the method takes control from the line in which the method is called to the address space of the method — after that, returning the method gives back control to the line which had invoked the method in the first place. The return keyword is an essential and frequently used part of the Java programming language (and in any programming language in general).

Java Return Keyword Tutorial

The return keyword is used to return from a method when its execution is complete. When the return statement is reached in the method, a program returns to the code that invoked it.

The method can return a value or reference type or does not return the value. If the method does not return a value, a method must be declared void, and it doesn’t need to contain the return statement.

If the method declares to return the value, then it must use a return statement within a body of the method. The data type of a return value must match the method’s declared return type.

Since the return is a keyword, it cannot be used as an identifier.

There are two ways in which the return keyword can be used with a method:

  1. When a method returns a value
  2. When a method does not return a value

#When the method returns a value

When a method defines a return type, for example, String, int, double, etc. , it is mandatory to have a return statement in the method.

class NewClass {

  int var1, var2;

  NewClass(int v1, int v2) {
    var1 = v1;
    var2 = v2;
  }

  int getFirst() {
    return var1;
  }

  int getSecond() {
    return var2;
  }
}

class ReturnEx1 {
  public static void main(String[] args) {
    NewClass obj = new NewClass(1, 2);

    int x, y;

    x = obj.getFirst();
    y = obj.getSecond();

    System.out.println("First: " + x);
    System.out.println("Second: " + y);
  }
}

See the following output.

 

Return Keyword in Java Example

Here, when the getFirst() method gets called in Line 26, the control goes to Line 10. Then when the return statement is encountered in Line 11, control is returned to Line 26 again. An exact similar thing happens for getSecond() method as well.

#When a method does not return the value

Methods which have the return type void, do not return a value and therefore, the return statement can be skipped in such cases.

But, one can use the return keyword intelligently to determine the flow of control of the program even while returning no value.

This scenario is demonstrated in the following example ReturnEx2.java.

import java.util.*;

class NewClass{
	void checkEven(int x){
		
		return;

		if(x%2==0)
			System.out.println( x + " is an even integer.");


	}
}


class ReturnEx2{
	public static void main(String [] args){
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter an integer: ");

		int x;

		x = sc.nextInt();

		NewClass check = new NewClass();
		check.checkEven(x); 
	}
}

See the output.

 

When the method does not return a value

Here, as soon as the checkEven() method is called in Line 22, the control is shifted to Line 4. Then the condition in the if the statement is checked and if the integer is odd, the return statement is encountered, and the control immediately shifts out of the method and back to Line 22 without even bothering to see what lies after the return statement in the checkEven() method.

In Java, there is a safeguard against the situation in which the programmer might, by an oversight, write some critical lines of code after a return statement which would lead to a logical inconsistency which might take days to debug in case of large projects.

The Java compiler throws a compilation error in such a case and prompts the message that there is code written after a return statement which is “unreachable.”

This is demonstrated in the following example program ReturnEx3.java

import java.util.*;

class NewClass{
	void checkEven(int x){
		
		return;

		if(x%2==0)
			System.out.println( x + " is an even integer.");


	}
}


class ReturnEx3{
	public static void main(String [] args){
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter an integer: ");

		int x;

		x = sc.nextInt();

		NewClass check = new NewClass();
		check.checkEven(x); 
	}
}

See the following output.

 

Return Keyword in Java Tutorial

In conclusion, the Java return keyword is essential and frequently used while dealing with methods, and it can also be used to direct the control flow as required.

Finally, Java Return Keyword Tutorial | Return Keyword in Java Example article is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.