File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion 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

KB page no 336 return statement

sumaraghavi ragha
Ranch Hand

Joined: Nov 17, 2006
Posts: 118

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

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
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()

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
I agree. Here's the link:
subject: KB page no 336 return statement
It's not a secret anymore!