Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java recursive calls

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java recursive calls

    I'm having a little bit of confusion on how recursive calls work.
    What does the method call recur2(5) display with the following code?:
    public static void recur2 (int n)
    {
      if(n<=0)
      { 
        return;
      }
      else
      {
        recur2(n - 2);
        System.out.print(n);
      }
    }

    I would think it would print "31" but that's not right?


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Java recursive calls

    Follow it through. The first call (n == 5) calls itself with 3 but 5 is pushed to be printed later. Then the call is passed 3, it is called with 1 but 3 is pushed to be printed later. Lastly, it is called with 1 which is less than zero and that stops the recursion but the print() is called with 1. So 135 is printed as 1 is that most recent call and on up the stack.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Java recursive calls

    What does the method call recur2(5) display with the following code?:
    Did you run the code? What is the output?

Similar Threads

  1. How to make calls to methods
    By ksahakian21 in forum Java Theory & Questions
    Replies: 4
    Last Post: April 6th, 2012, 10:57 PM
  2. Decimal To binary Recursive conversion java?
    By sirdonclemo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2011, 02:33 AM
  3. Identifying invalid calls
    By joshft91 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 26th, 2011, 04:38 PM
  4. Need help understanding method calls and such
    By hackman2007 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 14th, 2010, 08:18 AM
  5. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM