본문으로 건너뛰기
노리터자유게시판
여기 사실 휴대폰 커뮤니티임.성지 시세, 알뜰폰 요금 비교표직접 모아 가격순으로 보여줌10-20 확인 · 판매처 52곳 · 요금제 3,298개

자유게시판

아니 나도 반환값인건 아는데

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

이거 해석좀 ㅇㅇ

원서로 수업함 ㅠ

ㅋ로스
Signature

로스로스해

스크랩

댓글 4

  1. ㅈㅈ

    1/3정도 읽어는데 이거 다 읽어 주아햐마?

  2. 이해리

    외계어네요

  3. ㅋ로스

    ㅇㅇ

  4. 當_當

    リターン [return]

    1. [명사·サ변격 활용 자동사] 리턴.

    2. [명사·サ변격 활용 자동사] 되돌림. 복귀함.

댓글 작성

로그인 후 댓글을 작성할 수 있음.로그인

자유게시판

번호제목작성자작성일조회추천
3547826요번에 산 신발인데 졸라 마음에 드는데...[3]heh82조회175추천0
3547808계속 예의주시하고있는데[5]연달조회173추천0
3547802아 사람 만으니 찐다 쪄범조회278추천0
3547782야호 쿼티 별들어왓다[2]pOOhding조회220추천0
3547775절묘한 정덕인듯,,,꽃미녀추모중조회196추천0
3547772행시, 사시, 외시, 각종고시는꽃미녀추모중조회146추천0
3547765변태인 진범이가,, 상체길면[2]꽃미녀추모중조회207추천0
3547726연달횽[4]pOOhding조회203추천0
3547723ㅠㅠ꽃미녀추모중조회281추천0
3547720아니 나도 반환값인건 아는데[4]ㅋ로스조회219추천0
3547701아냥아 나 웨스턴랩 세미나 초대장 왔다 ㅇㅇ[2]ㅋ로스조회381추천0
3547694갤유의 단점을 말해줘랑[2]연달조회125추천0
3547683공부하러 ㅂㅂ꽃미녀추모중조회203추천0
3547664자바 좀 해본사람[5]ㅋ로스조회161추천0
3547653어머니 토욜날 가요 마요?[2]ㅋ로스조회198추천0
3547627그 중국누나가 막 내가 연락할꺼라고[1]꽃미녀추모중조회148추천0
3547620어제 중국사람이랑 국제전화함[3]꽃미녀추모중조회151추천0
3547593내눈이 잘못된거냐?[4]꽃미녀추모중조회330추천0
3547578아직까지 달구누님이 응답이 없는걸로봐선[2]꽃미녀추모중조회244추천0
3547563내가 대학교 1학년때 중국엘 갔는데꽃미녀추모중조회309추천0