0

I'm trying to learn java oop and i find some problem understanding why the use of post-increment in Recursive Method cause error ? I don't understand .

Main Class : public class Main {

    public static void main(String[] args) {
        A a = new A();
        a.res(0);
    }        
}

Code work fine :

public class A {

    public void res(int a){
        if (a < 5)
            res(a+1);   
        System.out.println(a);
    }
}

Output : run:

5

4

3

2

1

0 BUILD SUCCESSFUL (total time: 0 seconds)

But in when i use the ++ operator i got StackOverflowError .

public class A {

    public void res(int a){
        if (a < 5)
            res(a++);   
        System.out.println(a);
    }
}
  • It is not a duplicate. He is asking why post-increment is producing an error. – RaminS Dec 02 '16 at 21:54
  • 1
    @Gendarme Yes, but that is (IMO) in all likelihood to be traced back to a lack of basic understanding of the increment operators. – Siguza Dec 02 '16 at 21:59
  • Well, he obviously knows that it's called *"post-increment"*. I think it's more the fact that he doesn't realise that the increment happens *after the method returns*, which doesn't happen until the nested method returns, which doesn't happen until that method's nested method returns (etcetera) - so the increment actually never happens. – RaminS Dec 02 '16 at 22:02
  • @AlexanderPogrebnyak Nothing wrong with pre-increment, is there? – RaminS Dec 02 '16 at 22:09
  • @AlexanderPogrebnyak What the difference between a + 1 and a++ ? – stackoverflow User Dec 02 '16 at 22:18
  • ^ And that's where my dupe vote comes into play. – Siguza Dec 02 '16 at 22:24

4 Answers4

3

You are confusing a++ and ++a. As per java documentation - "The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value".

In your case the recursive method is always called with the argument 0

Curious
  • 453
  • 4
  • 15
  • When does the `++` actually happen? When the method returns (which never happens in this case)? – RaminS Dec 02 '16 at 21:55
  • @Gendarme It should be synonymous to `int b = a++; res(b);`. – Siguza Dec 02 '16 at 22:01
  • @Siguza It's not synonymous, because the fact that this is a recursive function is key. – RaminS Dec 02 '16 at 22:05
  • @Gendarme Run [this](http://pastebin.com/THqD7s4W). According to you it should give `0,0`, according to me `0,1`. – Siguza Dec 02 '16 at 22:19
  • @Gendarme For calling the method first of all the expression a++ is evaluated. As per the language spec it returns the pre increment value. This returned value is passed as argument to the method. But the same operation would also increment the value of a. Hope it explains. – Curious Dec 02 '16 at 22:36
  • Also its worth remembering that ++ operator is not Atomic and should not be used in a concurrent environment. – Curious Dec 02 '16 at 22:44
1

The post-increment operator increments the variable after you use it, so it will only increment the variable after the recursive function is returned.

Your code is doing this:

res(a++) //a = 0
//now a = 1

The recursion will never reach that next line, so the recursive function will always call res(0)

References: https://stackoverflow.com/a/2371162/7238307

Community
  • 1
  • 1
wschultz4
  • 51
  • 4
0

Well... what do you expect, exactly? Let's re-write your program slightly. Your program (the one that crashes) can effectively be re-written like so, achieving the same effect (that is, crashing due to a StackOverflowError):

public class A {
    public void res(int a){
        if (a < 5) {
            res(a);  
            ++a;
        }  
        System.out.println(a);
    }
}

Your variable a will only increment after the recursive call to res has completed... which is never, because you're always invoking res with the same value.

nasukkin
  • 2,460
  • 1
  • 12
  • 19
0

This is how the recusrive calls are resolved with a+1.

a=0, res(0)
+ 0 < 5, res(1)
++ 1 < 5, res(2)
+++ 2 < 5, res(3)
++++ 3 < 5, res(4)
+++++ 4 < 5, res(5)
++++++ 5 = 5, print 5
+++++ print 4
++++ print 3
+++ print 2
++ print 1
+ print 0

Regarding the problem of your code with a++ is that the increment is performed after the the recursive call is performed. So you basically get:

a=0, res(0)
+ 0 < 5, res(0)
++ 0 < 5, res(0)
+++ ...
NiVeR
  • 9,644
  • 4
  • 30
  • 35