Post-Incrementing/decrementing in recursive method calls (Java)

岁酱吖の 提交于 2019-12-10 20:02:17

问题


Say you have a recursive method, and you post-increment/decrement a value in the recursive call. Why will this result in a stack overflow exception when a pre-increment/decrement will not?

Ex.

numberCount(currentNumber++); //Stack overflow exception

numberCount(++currentNumber); //No stack overflow exception

Thanks in advance for any clarification.


回答1:


The first

numberCount(currentNumber++); //Stack overflow exception

is equivalent to:

numberCount(currentNumber);
currentNumber += 1;

while the the second

numberCount(++currentNumber); //No stack overflow exception

is equivalent to

currentNumber += 1;
numberCount(currentNumber);

Need I explain more?




回答2:


In case of numberCount(currentNumber++);, if an Exception is thrown by numberCount function, will the variable currentNumber incremented?



来源:https://stackoverflow.com/questions/16095176/post-incrementing-decrementing-in-recursive-method-calls-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!