Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:16:31+00:00 2026-05-23T21:16:31+00:00

I try very simple test: public static void main(String[] args) { test(2); } public

  • 0

I try very simple test:

public static void main(String[] args) {
     test(2);
}

public static void test(int i) {
    i--;
    System.out.println("i="+i);
    if (i < 0)
        return;

    System.out.println("test1");
    test(i);
    System.out.println("test2");
    test(i);
}

Output:

i=1
test1
i=0
test1
i=-1
test2
i=-1
test2
i=0
test1
i=-1
test2
i=-1

I cannot understand why variable i in second call (test2) has value 0 after that already has 0 ?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:16:32+00:00Added an answer on May 23, 2026 at 9:16 pm

    I find indenting the output will help explain these things, each level here corresponds to the depth of the call stack (how many recursions deep you are), and the sequence corresponds to when these things are executed:

    i=1
    test1
      i=0 (invoked on the test1 path)
      test1
        i=-1 (invoked on the test1 path)
      test2
        i=-1 (invoked on the test2 path)
    test2
      i=0 (invoked on the test2 path)
      test1
        i=-1 (invoked on the test 1 path)
      test2
        i=-1 (invoked on the test 2 path)
    

    Notice that under each indentation level there are are invocations under the headings “test1” and “test2”, this is because you recursively call test under each of these headings, and so at each execution of test you recurse twice.

    Let’s backup a bit and take a simpler case, if you were to execute test(1), you would expect to see:

    i=0
      test1
        i=-1
      test2
        i=-1
    

    Because you call test under the headings “test1” and “test2” you’re causing two paths to be navigated, one path under the “test1” heading, and a second under the “test2” heading.

    When you call test(2), your code roughly does this: (recursions omitted)

    (i = 2)
    Decrement i  (i = 1)
    Print i
    Print "test1"
    Call test(i) (test(1))
    Print "test2"
    Call test(i) (test(1))
    

    …and remember, each time you call test(1), your code roughly does this: (recursions omitted)

    (i = 1)
    Decrement i  (i = 0)
    Print i
    Print "test 1"
    Call test(i) (test(0))
    Print "test 2"
    Call test(i) (test(0))
    

    If you replace each call in the first block with the output of the test(1) block you’ll see it exactly generates your output.

    Basically, you get two i=0 prints because you recurse twice in each function call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To try the concept, I'm doing a very simple test. I got a form
Very simply put: I have a class that consists mostly of static public members,
This maybe very simple but I cant seem to sort it out on my
i wrote this simple example: //file TestController.java public interface TestController { public List<Test> findAll();
I just made a very simple test app using documentation from MSDN. All I
I'm new to Play and try some very simple things with AJAX. Right now
I'm doing a very simple test with queues pointing to the real Azure Storage
I try to create a very simple physics engine for my study (processing used
The very common beginner mistake is when you try to use a class property
When I try to make a very large boolean array using Java, such as:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.