Вопрос

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.

Это было полезно?

Решение

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?

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top