The so-called recursion, is oneself indirectly or directly to oneself.
Three conditions to be aware of recursion:
1. Boundary conditions
2, recursive forward paragraph
3. Recursive return segment
Recursive progression when boundary conditions are not met
When the boundary condition is met, the recursive return
Here's a small example:
public class Demo
{
public static void Main (String args[])
{
ToBin (6);
}
public static void toBin (int num)
{
if (num>0)
{
ToBin (NUM/2);
System.out.println (num%2);
}
}
}
The first step into the main function calls the Tobin function, 6>0, executes Tobin (3), 3>0, executes Tobin (1), 1>0, executes Tobin (0), the local boundary condition is not satisfied, Therefore executes the print statement in the last called ToBin (1) that satisfies the condition, and the result is that the 1,tobin (1) function ends, returns to the ToBin (3) that called the function, the result is 1, and returns to the ToBin (6) that called this function after the function is finished, and the result is 0, The final answer is 1,1,0.
This article is from the Java recursion blog, so be sure to keep this source http://9691015.blog.51cto.com/9681015/1589792
Java Recursive thinking