Use of return statement in Java

By: Emiley J Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(8)


1. View Comment

What is meant ClasspathResource why do we use it in Springs.

View Tutorial          By: anil kumar at 2008-06-03 05:41:36
2. View Comment

i am very much pleased in this type of tutorial

View Tutorial          By: arup adhikari at 2009-07-10 08:00:24
3. View Comment

Thank you so much for this. This helped me through my homework. Thank you. :)

View Tutorial          By: GeeAsphyxiated at 2009-08-31 08:03:07
4. View Comment

my quseation is why use we return statement
and what its work and examples of return statement in java .


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

When the Value Is Returned To The Method Then How Can We Print This Value. Actually I am trying To Print This Value NAd this Is giving Error That The variable is not initialized everytime.

View Tutorial          By: nitin deep singh at 2011-08-09 11:43:07
6. View Comment

Comment: Just for future readers: (apart from the annoying capulation the last reader used): toString(), make a method hereof:
public String toString() { String s = \"blablabla\" + w.e you want to add from the class + \"blabla \" + (you can add w.e you like aslong if its in the class); return s; } and where you want it get the toString() method, and System.out.println (or w/e you use like a label) and you can get it on your screen.


View Tutorial          By: Anonymous at 2013-05-17 08:53:20
7. View Comment

Where to use the return type int and how it is used???and where the return key word is used???suggest me.

View Tutorial          By: sohm chakraborty at 2015-07-01 06:52:15
8. View Comment

main method's return type is void .how main methad can return a value.

View Tutorial          By: sayyed moin at 2015-09-07 08:55:33

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
InetAddress Example program in Java
Read from a COM port using Java program
Stack example in Java - push(), pop(), empty(), search()
How to use ArrayList in Java
The Basic Structure of a Simple Java program
Using StringTokenizer in Java
FileReader and FileWriter example program in Java
Transient vs Volatile modifiers in Java
Vector example in Java
Method Overriding in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
append() in Java
Student marks calculation program in Java
Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
Text to Speech conversion program in Java
inheritance in Java
How to initialize an Array and how to copy the array
Using Charts in JasperReports
What is Unicode?
Operator Precedence in Java
Of Object, equals (), == and hashCode ()
DateFormat sample program in Java
A Simple whois program in Java
PushbackReader sample program in Java
CharArrayReader example program in Java
ByteArrayOutputStream - sample program in Java
Vector example in Java
Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals()
HashSet Sample program in Java