Unable to understand the output of this

152 views Asked by At

I have this code...

public class BackHanded {
int state = 0;

BackHanded(int s) {
    state = s;
}

public static void main(String... hi) {
    BackHanded b1 = new BackHanded(1);
    BackHanded b2 = new BackHanded(2);
    System.out.println(b1.go(b1) + " " + b2.go(b2));
}

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this);
    }
    return ++this.state;
   }
}

Why the statement return ++this.state; executed twice here in the second method call?

EDIT: I expected the output to be 2 6. But I got 2 7.

6

There are 6 answers

3
Kevin Bowersox On BEST ANSWER

The method calls itself within itself using recursion. The recursion does not occur on the first invocation since state != 2, however the second invocation satisfies the conditional this.state == 2 causing the method to recurse.

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this);
    }
    return ++this.state;
   }
}

The execution of the second invocation of this method b2.go(b2) occurs in this manner:

  1. The conditional is evaluated and since state ==2 we fall into the conditional block.
  2. Within the conditional block state is assigned to 5.
  3. The method then invokes itself (recursion).
  4. We start at the conditional once again, this time state != 2 so we skip the conditional.
  5. The method returns ++this.state or 6 and finishes the invocation of itself.
  6. Execution returns to the first invocation, which just finished executing the conditional block, so we head to the return statement ++this.state or 7.
0
Sakthi On
when the go(b2) is called
  checks(b2.state==2) is true then
    b2.state changes to 5 then
    on coming to return ++(b2) called
  this call will return value 6 now the return statement will be as ++(6) 
    on incrementing 6 it returns 7
0
Ashish Aggarwal On

Because in seconde case value of state is 2, So if (this.state == 2) becomes true then state = 5 and next line is go that means same method called again.

In this call state value is 5 and return ++this.state means state = 6, and return to the previous call

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this); //returned here
    }
    return ++this.state;
   }
}

and last return will be ++this.state means ++6 that means state = 7.

0
Zegar On

Its because of the recursion. First the return statement is called directly from System.out.println(b1.go(b1) + " " + b2.go(b2)); and then this method itself calls a go() method but this time if (this.state == 2) is not fulfilled so recursion breaks at this second call.

And this way you can obtain such a callstack:

call go(b) //b.state == 2
  call go(b) //b.state == 5
  return ++this.state;
return ++this.state;

As it was stated in Arnaud Denoyelle's answer.

0
Ceiling Gecko On

Since the second BackHanded instance b2 has a value of 2, once it enters the method go it checks if the state is 2 and since it is, it executes another instance of go, now since state is no longer 2 it executes return ++this.state; after it finishes, it returns to the first call of go (since it didnt finish it yet and executes return ++this.state; and completes the first go method call.

If you want for it to execute only once add a break call inside the if statement.

int go(BackHanded b) {
    if (this.state == 2) {
        b.state = 5;
        go(this);
        break;
    }
    return ++this.state;
    }
}
0
Devolus On

b1 is initialized with 1 and you do

System.out.println(b1.go(b1) + " " + b2.go(b2));

So first look at b1.

Value is 1, the if statement doesn't trigger, the ++ is executed and the output is 2.

Now to b2 which is initialized with 2.

The if is triggered because the value is 2, value set to 5 and you call recursivly again. Now the value is 5, the if doesn't trigger and the value is increased to 6. The function returns to after the recursive call, at which point the value was already 6 and is again increased to 7. Output is 7.