0

Can someone explain what's happening in this code. The code terminates only when -1 is passed. Before that it keeps asking for user input. And only after termination, it prints multiples of 3 from the input values. I am confused where all of the values of x are stored before termination.


public class Recursion {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        multipleThree(in);


    }//end of main
    public static  void multipleThree(Scanner in) {
        
        System.out.println("Enter the list(-1 to terminate):");
        int x = in.nextInt();
        if (x == -1) {
            return;
        } else {
            multipleThree(in);
            if (x % 3 == 0)
                System.out.println(x + " is divisible by 3");

        }//end of else
      }//end of method
    }//end of class

1
  • every time a method is called, some memory is reserved for its variables and parameters (often called a frame) - so each call has its own version of the variables - since int x declares a variable inside the method, this is saved in that memory (frame). Not only relevant for recursive calls, but key for working of recursive methods. [since output is done after the call to multipleThree, it will only be executed after the call(s) return] Commented Mar 1, 2021 at 11:54

1 Answer 1

1

I am confused where all of the values of x are stored before termination.

They are stored on the call stack.

In the beginning, the call stack only has main. Then main calls multipleThree. This pushes a new frame onto the call stack. The frame will store things about where this call to multipleThree has progressed to, what local variables there are, etc.

Let's say you entered 3. x, which is stored on the frame that is currently the top of the stack. The else branch gets executed, and multipleThree gets called again, so a new frame gets pushed onto the call stack. Note that we are not done with the first call to multipleThree yet. We can come back to it when we finished executing this second call to multipleThree. Remember that the 3 you entered is still stored on the call stack.

Let's say you entered 5 next, the same thing happens - the number 5 gets stored on the top of the stack, then a new frame gets pushed as you call multipleThree again (this is now the third time).

Finally, you enter -1. At this point, the stack looks like this:

multipleThree (x = -1)
multipleThree (x = 5)
multipleThree (x = 3)
main

You can see this in the debugger on your IDE. e.g. in IntelliJ IDEA

enter image description here

Because you entered -1, this call to multipleThree actually finishes because it reaches the return statement. The top of the stack now gets popped, and we go back to executing the second call to multipleThree. The stack now looks like this:

multipleThree (x = 5)
multipleThree (x = 3)
main

In the second call, remember that x is 5 - not divisible by 3, so we don't do anything. Now we are done with the second call to multipleThree, so the stack gets popped again:

multipleThree (x = 3)
main

Now we finally go back to executing the first call to multipleThree, when x is 3. 3 is divisible by 3, so we print it. Now we are done with the first call of multipleThree so we pop it off the stack too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.