Summary: This section describes the definition of static methods, the results of invoking static methods, the nature of several methods, recursion, the basic programming model, and the meaning of modular programming.
Focus:
1. All Java programs in this book are either definitions of data types or a library of static methods. In many languages, static methods are called functions because they are similar in character to numeric functions.
2. The method requires a parameter (a value of a data type) and calculates the return value of a data type (such as the result of a mathematical function) based on the parameter, or produces some side effect (such as printing a value).
3. The method handles the value of the parameter, not the parameter itself. The result of this approach is that changing the value of a parameter variable in a static method has no effect on the caller.
Value passing also means that the array parameter will be the alias of the original--the parameter variable used in the method can refer to the caller's array and change its contents (just cannot change the original array variable itself).
Public classTestarray { Public Static voidMain (string[] args) {Double[] A; A=New Double[5]; Sort (a); for(inti = 0; i < a.length; i++) {System.out.println (a[i]); } } Public Static voidSortDoubled[]) { for(inti = 0; i < d.length; i++) {D[i]= 5.0; } }}
In the above example, the contents of the original array A are changed because sort () passes in the alias D of array A and changes the contents of D.
4. Reasons for using recursion:
· The recursive code is more concise, elegant, and understandable than the corresponding non-recursive code.
· We can use mathematical models to estimate the performance of the program.
5. The following three points are most important when writing recursive code:
· Recursive which comes has one of the simplest cases-the first statement of a method is always a conditional statement that contains a return.
· Recursive calls always try a smaller sub-problem so that recursion converges to the simplest case.
· There should be no intersection between the parent of the recursive call and the child problem that is trying to resolve.
6. A static method library is a set of static methods (including a main () method) defined in a Java class.
7. Benefits of modular Programming (P15)
Algorithm (4th edition) -1.1.6 static method