Use of return statement in Java
By: Emiley J
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.
Archived Comments
1. It was helpful thnx
View Tutorial By: Ankita Banerjee at 2016-12-13 02:56:55
2. why we using return type in java program
View Tutorial By: sudarshan at 2016-03-15 03:09:31
3. 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
4. Where to use the return type int and how it is used???and where the return key word is used???sugges
View Tutorial By: sohm chakraborty at 2015-07-01 06:52:15
5. Thanks for the info
View Tutorial By: msoner at 2015-04-29 19:19:43
6. Comment: Just for future readers: (apart from the annoying capulation the last reader used): toStrin
View Tutorial By: Anonymous at 2013-05-17 08:53:20
7. how can I return to the if statement again?
View Tutorial By: raquel at 2012-07-05 09:12:20
8. When the Value Is Returned To The Method Then How Can We Print This Value. Actually I am trying To P
View Tutorial By: nitin deep singh at 2011-08-09 11:43:07
9. what is default return type of return statement
View Tutorial By: vahid at 2011-03-11 04:56:11
10. can we use return statement without if
View Tutorial By: Pardeep at 2011-02-04 21:39:06
11. This was extremely helpful, cheerrs.
View Tutorial By: free samples and loops at 2011-01-25 07:28:14
12. my quseation is why use we return statement
and what its work and examples of return stateme
View Tutorial By: pankaj kumar at 2010-08-31 03:53:48
13. 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
14. i am very much pleased in this type of tutorial
View Tutorial By: arup adhikari at 2009-07-10 08:00:24
15. What is meant ClasspathResource why do we use it in Springs.
View Tutorial By: anil kumar at 2008-06-03 05:41:36
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program