Java Reference
In-Depth Information
which (because n is equal to 123 ) is the call
writeVertical(123 / 10);
which is equivalent to
writeVertical(12);
When execution reaches this recursive call, the current method computation is placed
in suspended animation and this recursive call is executed. When this recursive call is
finished, the execution of the suspended computation will return to this point and the
suspended computation will continue from this point.
The recursive call
writeVertical(12);
is handled just like any other method call. The argument 12 is substituted for the
parameter n , and the body of the method is executed. After substituting 12 for n , there
are two computations, one suspended and one active, as follows:
if (123 < 10)
{
System.out.println(12);
}
else //n is two or more digits long:
{
writeVertical(12 / 10);
System.out.println(12 % 10);
}
if (12 < 10)
{
System.out.println(12);
}
else //n is two or more digits long:
{
writeVertical(12 / 10);
System.out.println(12 % 10);
}
Computation will stop here
until the recursive call
returns.
Because 12 is not less than 10 , the else part is executed. However, as you already
saw, the else part begins with a recursive call. The argument for the recursive call is
n/10 , which in this case is equivalent to 12/10 . So, this second computation of the
method writeVertical is suspended, and the following recursive call is executed:
writeVertical(12 / 10);
which is equivalent to
writeVertical(1);
Search WWH ::




Custom Search