1, What is the method?
A Java method is a collection of statements that together perform a function
- Method is an ordered set 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
- Is a piece of code used to complete a specific function
2, Method definition and call
Method definition: it is a piece of code used to complete a specific function
Syntax: a method contains a method header and a method body.
- Modifier: This is optional, tells the compiler how to call the method, and defines the access type of the method.
- Return value type: a method may return a value. returnValueType is the data type of the return value of the method. Some methods perform the required operations but do not return a value. In this case, returnValueType is the keyword void.
- Method name: the actual name of the method. The method name and parameters together constitute the method signature.
- Parameter type: a parameter is like a placeholder. When a method is called, it passes a value to the parameter. This value is called an argument or variable. The parameter list refers to the parameter type, order and number of parameters of the method. The parameters are optional. The method can not contain any parameters.
-
- Formal parameter: used to receive external input data when the method is called.
- Argument: the 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.
Method call: object name. Method name (argument list)
public class Demo02 { public static void main(String[] args) { int c= max(12,21); System.out.println(c); } //How to compare sizes public static int max(int a,int b){ int result=0; if(a==b){ System.out.println("a be equal to b"); return 0;//Termination method } if (a>b){ result= a; }else{ result=b; } return result; } }
3, Method overloading
Overloading is a function with the same function name but different formal parameters in a class.
Rules for overloaded methods:
- Method names must be the same.
- The parameter list must be different (different number or type, different parameter arrangement order, etc.).
- The return types of methods can be the same or different.
- Only the return type is different enough to be an overload of the method.
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
public class Demo02 { public static void main(String[] args) { double c= max(12,21); System.out.println(c); } //How to compare sizes public static double max(double a,double b){ double result=0; if(a==b){ System.out.println("a be equal to b"); return 0;//Termination method } if (a>b){ result= a; }else{ result=b; } return result; } //How to compare sizes public static int max(int a,int b){ int result=0; if(a==b){ System.out.println("a be equal to b"); return 0;//Termination method } if (a>b){ result= a; }else{ result=b; } return result; } }
4, Command line parameters
public class CommandLine { public static void main(String args[]){ for(int i=0; i<args.length; i++){ System. out. println("args["+ i + "]:"+ args[i]); } } }
5, Variable parameters
public class Demo08 { public static void main(String args[]) { //Calling methods with variable parameters printMax(15,22.0,36,25.3); printMax(new double[]{1, 2, 3});//Array in nature } public static void printMax(double... numbers) { if (numbers.length == 0) { System.out.println("No argument passed"); return; } double result = numbers[0]; //sort! for (int i = 1; i < numbers.length; i++) { if (numbers[i] > result) { result = numbers[i]; } } System.out.println("The max value is " + result); } }
6, Recursion
Suggestion: try to use recursion
- Recursion is: method A calls method A! Is to call yourself
- 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 few programs, 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.
- 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.
public class MthodDemo01 {
//3*2*1 public static void main(String[] args) { System.out.println(f(3)); } public static int f(int n){ if (n==1){ return 1; }else { return n*f(n-1); } } }