O'Reilly logo

Java, A Beginner’s Guide, 5th Edition by Herbert Schildt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Returning from a Method

In general, there are two conditions that cause a method to return—first, as the range( ) method in the preceding example shows, when the method’s closing curly brace is encountered. The second is when a return statement is executed. There are two forms of return—one for use in void methods (those that do not return a value) and one for returning values. The first form is examined here. The next section explains how to return values.

In a void method, you can cause the immediate termination of a method by using this form of return:

return;

When this statement executes, program control returns to the caller, skipping any remaining code in the method. For example, consider this method:

Here, the for loop will only run from ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required