This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes KB page no 336 return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "KB page no 336 return statement" Watch "KB page no 336 return statement" New topic
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: KB page no 336 return statement