1, the concept of recursion:
That calls itself recursively , each invocation, passing different variables, recursive help solve complex problems, while allowing code becomes simple .
2, the recursive call mechanism
Printing problems
com.ethjava Package; public class diguidayin { public static void main (String [] args) { Test (. 4); } public static void Test (n-int) { IF (n-> 2) { Test (. 1-n-); } System.out.println ( "Print n-:" n-+); } } // Print n-: 2 // Print n-:. 3 // Print n: 4
Recursive call rules:
1) When the program executes a method, it will open up a separate space: Stack
2) data of each space (local variables) are independent and do not affect each other
2) is as follows:
Recursion to solve the problem:
1. algorithms: merge sort, quick sort, binary search
2, mathematical problem: the Towers of Hanoi problem, factorial problem, 8 Queen
3, to solve the problem with a stack of: recursively resolve will be very simple.
Recursive need to comply with the principles of:
1) the implementation of a method, it creates a new protected independent space.
2) data for each process (local variables) are independent and do not affect each other
3) If the variable is a reference type (such as arrays) used in the method, reference will be shared by the type of data stored in the heap.
4)递归必须向退出递归的条件逼近,否则就是无线递归,出现StackOverflowError,
5) 当一个方法执行完毕后,或者遇到return,就会返回到调用的位置,继续往下执行。
遵守规则:谁调用,就将结果返回给谁,同时当方法执行完毕或者返回时,该方法也就执行完毕。