recursion – StackOverFlow in Postfix Decrement in JAVA

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.

Read more here: Source link