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); } } 
6
  • It is not a duplicate. He is asking why post-increment is producing an error. Commented Dec 2, 2016 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. Commented Dec 2, 2016 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. Commented Dec 2, 2016 at 22:02
  • @AlexanderPogrebnyak Nothing wrong with pre-increment, is there? Commented Dec 2, 2016 at 22:09
  • @AlexanderPogrebnyak What the difference between a + 1 and a++ ? Commented Dec 2, 2016 at 22:18

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

When does the ++ actually happen? When the method returns (which never happens in this case)?
@Gendarme It should be synonymous to int b = a++; res(b);.
@Siguza It's not synonymous, because the fact that this is a recursive function is key.
@Gendarme Run this. According to you it should give 0,0, according to me 0,1.
@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.
|
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

Comments

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.

7 Comments

@nasuhkkin i think i understand your answer . So the line ++a never reached to change the 0 value , cause of the function
@AyoubHit Yes it never reaches, because this is a recursive function and each res() calls another res(), and it goes on forever (i.e. the method never returns) because the value of a is always the same (0).
@Gendarme and when i use res(a+1); is not the same ?
@AyoubHit It is not the same. res(a+1) would work (res(++a) would work too). Try it.
@Gendarme i had try it and i got different result . The problem i had think that a++ work like a+1
|
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) +++ ... 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.