Use of return statement in Java

By: Emiley J Printer Friendly Format    

The last control statement is return. The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. As such, it is categorized as a jump statement. A brief look at return is presented here.

At any time in a method the return statement can be used to cause execution to branch back to the caller of the method. Thus, the return statement immediately terminates the method in which it is executed. The following example illustrates this point. Here, return causes execution to return to the Java run-time system, since it is the run-time system that calls main( ).

// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");

The output from this program is shown here:

Before the return.

As you can see, the final println( ) statement is not executed. As soon as return is executed, control passes back to the caller.

One last point: In the preceding program, the if(t) statement is necessary. Without it, the Java compiler would flag an "unreachable code" error, because the compiler would know that the last println( ) statement would never be executed. To prevent this error, the if statement is used here to trick the compiler for the sake of this demonstration.

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. What is meant ClasspathResource why do we use it i
View Tutorial          By: anil kumar at 2008-06-03 05:41:36

2. i am very much pleased in this type of tutorial
View Tutorial          By: arup adhikari at 2009-07-10 08:00:24

3. Thank you so much for this. This helped me through
View Tutorial          By: GeeAsphyxiated at 2009-08-31 08:03:07

4. my quseation is why use we return statement

View Tutorial          By: pankaj kumar at 2010-08-31 03:53:48

5. This was extremely helpful, cheerrs.
View Tutorial          By: free samples and loops at 2011-01-25 07:28:14

6. can we use return statement without if
View Tutorial          By: Pardeep at 2011-02-04 21:39:06

7. what is default return type of return statement
View Tutorial          By: vahid at 2011-03-11 04:56:11

8. When the Value Is Returned To The Method Then How
View Tutorial          By: nitin deep singh at 2011-08-09 11:43:07

9. how can I return to the if statement again?
View Tutorial          By: raquel at 2012-07-05 09:12:20

10. Comment: Just for future readers: (apart from the
View Tutorial          By: Anonymous at 2013-05-17 08:53:20

11. Thanks for the info
View Tutorial          By: msoner at 2015-04-29 19:19:43

12. Where to use the return type int and how it is use
View Tutorial          By: sohm chakraborty at 2015-07-01 06:52:15

13. main method's return type is void .how main methad
View Tutorial          By: sayyed moin at 2015-09-07 08:55:33

14. why we using return type in java program
View Tutorial          By: sudarshan at 2016-03-15 03:09:31

15. It was helpful thnx
View Tutorial          By: Ankita Banerjee at 2016-12-13 02:56:55