Java foundation 05 method
A Java method is a collection of statements that together perform a function.
- Method is an ordered combination of steps to solve a class of problems
- Method is contained in a class or object
- Methods are created in the program and referenced elsewhere
- Principle of design method: the original intention of method is function block, which is the collection of statement blocks to realize a certain function. When designing a method, we'd better keep the atomicity of the method, that is, a method only completes one function, which is conducive to our later expansion.
Definition of method
Java methods are similar to functions in other languages. They are code fragments used to complete specific functions. Generally, defining a method includes the following syntax:
Method contains a method header and a method body. The following are all parts of a method:
- Modifier: modifier, which is optional and tells the compiler how to call the method. Defines the access type of the method.
- Return value type: the method may return a value. returnValueType is the data type of the return value of the method. Some methods perform the required operation but do not return a value. In this case, returnValueType is the keyword void.
- Method name: is the actual name of the method. The method name and parameter table together constitute the method signature.
- Parameter type: the parameter is like a placeholder. When the method is called, the value is passed to the parameter. This value is called an argument or variable. Parameter list refers to the parameter type, order and number of parameters of a method. Parameters are optional, and methods can contain no parameters. (formal parameter: used to receive external input data when the method is called; actual parameter: data actually passed to the method when the method is called)
- Method body: the method body contains specific statements that define the function of the method.
Modifier return value type method name(Parameter type parameter name){ ··· Method body ··· return Return value; }
For example, define a simple summation method add():
public class Demo01 { //main method public static void main(String[] args) { //Actual parameter: the parameter passed by the actual call int sum = add(1,2); System.out.println(sum); } //addition //Formal parameter, used to define the function public static int add(int a,int b){ return a + b; } }
In this code, call the add() method through the sixth line of code, pass in the parameters 1 and 2, assign 1 to a, assign 2 to b, and return a+b to sum.
Another example is to define a simple size ratio method max():
public class Demo02 { public static void main(String[] args) { int max = max(10,20);//Call method System.out.println(max); } //Specific size public static int max(int num1,int num2){ int result = 0; if(num1 == num2){ System.out.println("num1 == num2"); return 0;//Termination method } if(num1 > num2){ result = num1; }else { result = num2; } return result; } }
Method call
- Calling method: object name Method name (argument list)
- Java supports two methods of calling methods, which can be selected according to whether the method returns a value.
- When a method returns a value, the method call is usually regarded as a value, such as int larger = max(30,40);
- If the return value of the method is void, the method call must be a statement: system out. println("hello world!");
Method overload
1. Overloading is a function with the same function name but different formal parameters in a class.
2. Rules for overloaded methods:
- Method names must be the same.
- The parameter list must be different (different number, different type, different parameter arrangement order, etc.).
- The return types of methods can be the same or different.
- Just different return types are not enough to overload methods.
3. Implementation theory:
If the method names are the same, the compiler will match them one by one according to the number of parameters and parameter types of the calling method to select the corresponding method. If the matching fails, the compiler will report an error.
For example, it is defined as follows in the code of the size ratio in the previous paragraph:
public class Demo02 { public static void main(String[] args) { int max = max(10,20);//Call method //double max = max(10,20); System.out.println(max); } //Ratio size (return double type) public static double max(double num1,double num2){ double result = 0; if(num1 == num2){ System.out.println("num1 == num2"); return 0;//Termination method } if(num1 > num2){ result = num1; }else { result = num2; } return result; } //Specific size public static int max(int num1,int num2){ int result = 0; if(num1 == num2){ System.out.println("num1 == num2"); return 0;//Termination method } if(num1 > num2){ result = num1; }else { result = num2; } return result; } }
When running int max = max(10,20); When running, the max() method that returns int type is called. When running double max = max(10,20); The max() method that returns the double type is called.
Command line parameter passing (understand)
Sometimes you want to pass messages to a program when it runs. This is achieved by passing command-line arguments to the main() function.
public class Demo03 { public static void main(String[] args) { //args.length array length for (int i = 0;i < args.length;i++){ System.out.println("args[" + i + "]:" + args[i]); } } }
Variable parameter (indefinite parameter)
In the method declaration, add an ellipsis (···) after the specified parameter type
Only one variable parameter can be specified in a method. It must be the last parameter of the method. Any ordinary parameter must be declared before it.
For example:
public class Demo04 { public static void main(String[] args) { Demo04 demo04 = new Demo04(); demo04.test(1,2,3,4,5,6,7,8,9); } public void test(int ... i){ System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]); System.out.println(i[3]); } }
recursion
1. Recursion, that is, method A calls method A and calls itself
2. Using recursion, we can solve some complex problems with simple programs. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem. The recursive strategy can describe the multiple repeated calculations required in the problem-solving process with only a small amount of program, which greatly reduces the amount of code of the program. The ability of recursion is to define an infinite set of objects with limited statements.
3. The recursive structure consists of two parts:
- Recursive header: when not to call its own method. If there is no head, it will fall into a dead cycle.
- Recursive body: when to call its own method.
Example: Factoring
public class Demo05 { public static void main(String[] args) { //2! 2*1 //5! 5*4*3*2*1 System.out.println(f(5)); } public static int f(int n){ if (n == 1){ return 1; }else { return n*f(n-1); } } }
4. Recursive defect
Because Java uses the stack mechanism, the space of the stack is limited. Recursion will bring a large number of function calls, and too much data may lead to memory collapse, resulting in a lot of additional time overhead. The greater the depth of recursion, the larger the space and memory occupied. Too many recursive calls will affect the machine performance. Recursion is not a good algorithm.