StackOverFlow in Postfix Decrement in JAVA

77 Views Asked by At

This works fine because we are using the prefix decrement operator in the recursive call !

public static void main(String[] args) {
    doubt1(5);
}

Prefix Decrement:

static void doubt(int num)
{
    if(num==0)
        return;
    System.out.println(num);
    doubt(--num);
}

Postfix Decrement:

static void doubt1(int num)
{
    if(num==0)
        return;
    System.out.println(num);
    doubt1(num--);
}

But when we use the postfix decrement it can lead to stack over flow error , I understand why it's causing the error , but I want to know , if we use num-1 in the recursive call it doesn't give the overflow error , my question is -> why it's not giving the error coz it calculating the num-1 and passing the value to the recursive call but not calculating the num-- in the call ?

Want the explanation why it calculating the num-1 not num-- in the recursive call ? Not why it's happening !

3

There are 3 best solutions below

0
Thomas Kläger On
    doubt1(num--);

is internally implemented as

    int tmp = num;
    num = num - 1;
    doubt1(tmp);

That is, it recursively calls the doubt1 method with the original value and therefore the condition num == 0 is never reached.

0
John Bollinger On

When you invoke a method, the arguments are evaluated, and the resulting values are assigned as the initial values of the method's parameters. The expressions num-- and --num have the same side effect on num, but that is immaterial to the method invocation. The two evaluate to different values: the former to the value of num before it is decremented, and the latter to the value of num after it is decremented. The order of the operator relative to the operand is mnemonic for that.

Thus, with

int n = 1;
some_method(n--);

the value passed to some_method() is 1, but with

int n = 1;
some_method(--n);

the value passed to some_method() is 0.

In both cases, however, the value 0 is stored in n before execution of the body of some_method() commences.

0
Reilas On

First, with the unary decrement operator, the operation is occurring after, or before, the statement.
As opposed to after, or before, the expression.

So, in this situation, there will be a difference between num-- and --num.

  • doubt(num--) is decrementing after the call statement, and not after the parameter expression.

  • double(--num), the opposite is happening, the decrement is happening before the parameter expression.

Essentially, from a compiler's stand-point, for the doubt(--num) statement, you have the following.

  1. Get num
  2. Decrement num
  3. Apply num as doubt parameter
  4. Execute doubt with parameter value

Alternately, with doubt(num--) you have the following.

  1. Get num
  2. Apply num as doubt parameter
  3. Decrement num (which has no effect, since the parameter value was already set)
  4. Execute doubt with parameter value

Essentially, for the doubt(num--) statement, after the compiler has num, and is ready to call doubt, it will then decrement num.
As opposed to having num, then decrementing, and then calling doubt.