This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "return statement" Watch "return statement" New topic
Author

return statement

Jim Colwood
Greenhorn

Joined: Jul 10, 2007
Posts: 10
Hi all,

any idea why this would return "Sum = 15" and not "Sum = 1"?



couldn't find a similar thread with my brief search in the forum.
Thanks.


SCJP 6 |
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I was wrong, so I deleted this post. Don't want false information floating out there confusing everyone! See below post for correct answer.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Calculate calls itself recursively, so:
Calculate(5) = Calculate(4) + 5
= Calculate(3) + 4 + 5
= Calculate(2) + 3 + 4 + 5
= Calculate(1) + 2 + 3 + 4 + 5
= 1 + 2 + 3 + 4 + 5
= 15


All code in my posts, unless a source is explicitly mentioned, is my own.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3019
    
  35

Good question. Please QuoteYourSources.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Jim Colwood
Greenhorn

Joined: Jul 10, 2007
Posts: 10
Thanks for your replies guys.

Yes Joe, I had my doubts about your reply. so I added couple of lines in the code to see what's going on at runtime.



Ruban, your calculation is right. but the real question i have is, even if the FIRST return statement returns the int, why is the returned value 15?

I had a question with similar logic in a Java test offered to me by a recruiter.
Thanks again.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Ruban, your calculation is right. but the real question i have is, even if the FIRST return statement returns the int, why is the returned value 15?


The FIRST return statement did return an int, but not to the main() method -- it returned it back to the calculate() method, which called it. If the main() method had triggered the FIRST return statement, then it would have printed 1.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Colwood
Greenhorn

Joined: Jul 10, 2007
Posts: 10
Duh! it seems simple after you explained it, but it's easy to miss it.

Thanks a lot Henry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return statement
 
Similar Threads
Fibonacci Sequence Problem
Problems calculating 2^n with For loop
Happy/unhappy numbers
Counting in Array List/ Standard deviation
What Are Good Tests ?