| Author |
KB page no 336 return statement
|
sumaraghavi ragha
Ranch Hand
Joined: Nov 17, 2006
Posts: 118
|
|
Hi friends please explain me the following code why it is printing the output one time?? static boolean doStuff() { for (int x = 0; x < 3; x++) { System.out.println("in for loop"); return true; } return true; } Thanks in advance
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
|
A return statement causes execution to immediately leave the method.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
vaibhav mishra
Ranch Hand
Joined: Jun 18, 2008
Posts: 168
|
|
since your method signature says that method should return boolean so as soon as your method encounters a return method it's task is completed and it returns true and exit form method however if you remove first return statement you will get what you expect boolean yourMethod()
|
SCJP
|
 |
chander shivdasani
Ranch Hand
Joined: Oct 09, 2007
Posts: 206
|
|
As soon as the return statement is encountered, the control is passed to the block that called this method. It is immaterial to place the return statement in a for loop as it will be executed only once.
|
Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
|
 |
 |
|
|
subject: KB page no 336 return statement
|
|
|