return Statements
A return statement is a jump statement that is used to exit a method:
void doSomething() {
// Pseudocode.
do whatever is required by this method ...
return;
}
Whenever a return statement is encountered, the method stops executing as of that line of
code, and execution control immediately returns to the code that invoked the method in the
first place.
For methods with a return type of void, the return keyword is used by itself, as a complete
statement:
return;
However, it turns out that for methods with a return type of void, the use of a return; statement
is optional. If omitted, a return; statement is implied as the last line of the method. That is,
the following two versions of method doSomething are equivalent:
void doSomething() {
int x = 3;
int y = 4;
int z = x + y;
}
and
void doSomething() {
int x = 3;
int y = 4;
int z = x + y;
return;
}
The bodies of methods with a non-void return type, on the other hand, must include at
least one explicit return statement. The return keyword in such a case must be followed by an
expression that evaluates to a value compatible with the method’s declared return type. For
example, if a method is defined to have a return type of int, then any of the following return
statements would be acceptable:
return 0; // returning a constant integer value
return x; // returning the value of x (assuming that x
// has previously been declared to be an int)
return x + y; // returning the value of the expression "x + y" (here,
// we're assuming that "x + y" evaluates to an int value)
return (int) z; // casting the value of z (assume z was declared as a double)
// to an int value
and so forth. As another example, if a method is defined to have a return type of boolean, then
any of the following return statements would be acceptable:
return false; // returning a boolean constant value
return outcome; // returning the value of variable outcome
// (assuming that outcome has previously been
// declared to be of type boolean)
return (x < 3); // returning the boolean value that results when
// the (numeric) value of x is compared to 3:
// if x is less than 3, this method returns a
// value of true; otherwise, it returns false.
A method body is permitted to include more than one return statement. Good programming
practice, however, is to have only one return statement in a method, at the very end. Let’s look once
again at the isHonorsStudent method discussed previously, which has two return statements:
boolean isHonorsStudent() {
if (gpa >= 3.5) {
return true; // first return statement
}
else {
return false; // second return statement
}
}
Let’s rewrite this method to use a locally declared boolean variable, result, to capture the
true/false answer that is to ultimately be returned. We’ll return the value of result with a single
return statement at the very end of the method:
boolean isHonorsStudent() {
// Declare a local variable to keep track of the outcome; arbitrarily
// initialize it to false.
boolean result = false;
if (gpa >= 3.5) {
// Instead of returning true, we record the value in our "result"
// variable:
result = true;
}
else {
// Instead of returning false, we record the value in our "result"
// variable:
result = false;
}
// We now have a single return statement at the end of our method to return the
// result.
return result;
}
As it turns out, since we initially assigned the value false to result, setting it to false
explicitly in the else clause is unnecessary; we could therefore simplify the isHonorsStudent
method as follows:
boolean isHonorsStudent() {
// Declare a local variable to keep track of the outcome; arbitrarily
// initialize it to false.
boolean result = false;
if (gpa >= 3.5) {
result = true;
}
// Note that we've removed the 'else' clause ... if the "if" test
// fails, variable "result" already has a value of false.
return result;
}
There is, however, one situation in which multiple return statements are considered
acceptable, and that is when a method needs to perform a series of operations where failure
at any step along the way constitutes failure as a whole. This situation is illustrated via
pseudocode:
// Pseudocode.
boolean someMethod() {
// Perform a test ... if it fails, we wish to abort the method as
// a whole.
if (first test fails) return false;
// If we pass the first test, we do some additional processing ...
do something interesting ...
// Then, perhaps we perform a second test, where again failure of the
// test warrants immediately giving up in our "quest."
if (second test fails) return false;
// If we pass the second test, we do some additional processing ...
// details omitted.
// If we reach this point in our code, we return a value of true
// to signal that we made it to the finish line!
return true;
}
Note that the Java compiler will verify that all logical pathways through a method return
an appropriately typed result. For example, the following method will generate a compiler error
because a proper return statement will only be reached if the if test succeeds; if the if test fails,
the return statement is bypassed:
boolean xGreaterThanThree(int x) {
if (x <= 3) return false;
}
The specific compiler error message in this case would be as follows:
missing return statement:
boolean xGreaterThanThree
이거 해석좀 ㅇㅇ
원서로 수업함 ㅠ

댓글 (4)
1/3정도 읽어는데 이거 다 읽어 주아햐마?
외계어네요
ㅇㅇ