1, What is method
- System.out.println(), so what is it?
A: System is "class". out is "output object". println() is "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 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.
package method; public class Demo01 { //main method public static void main(String[] args) { //Actual parameters: the parameters passed to him by the actual call int sum = add(1, 2); System.out.println(sum); System.out.println("===================================="); text(); } //addition //Formal parameter: used to define the function public static int add(int a,int b) { return a+b; } public static void text() { for (int i=1;i<=1000;i++){ if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){ System.out.println(); //System.out.println("\n"); } } } }
2, Method definition and call
Definition of method
-
Java methods are similar to functions in other languages. They are a piece of code = = used to complete specific functions. Generally, defining a method includes the following syntax:
-
==Method contains a method header and a method body== Here are all parts of a method:
- Modifier: modifier, which is optional, tells the compiler how to call the method. 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: = = 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 a method is called, a 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.
- 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.
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-STnSiTrN-1635595853510)(C:\Users\wangcheng\Desktop\MarkDown learning \ image \ method definition. png)]
Method call
-
Calling method: object name. Method name (argument list)
-
Java supports two methods of calling methods, which are selected according to whether the method returns a value.
-
When a method returns a value, the method call is usually treated as a value. For example:
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,kuangshen!");
-
Expand your understanding after class: value passing (Java) and reference passing.
Java is value passing.
//See the following section for the code.
3, Method overloading
-
Overloading is a function with the same function name but different formal parameters in a class.
-
Rules for method overloading:
- 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.
-
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.
package method; public class Demo02 { public static void main(String[] args) { int max01 = max(10, 20); double max02 = max(10.0, 20.0); int max03 = max(10, 20,30); int max04 = max(10, 20.0 ); System.out.println(max01); System.out.println(max02); System.out.println(max03); System.out.println(max04); } //Specific size public static int max(int a,int b) { int result=0; if (a==b){ System.out.println("a==b"); return 0;//Termination procedure } if (a>b){ result=a; } if (a<b){ result=b; } return result; } //Specific size public static double max(double a,double b) { double result=0; if (a==b){ System.out.println("a==b"); return 0;//Termination procedure } if (a>b){ result=a; } if (a<b){ result=b; } return result; } //Specific size public static int max(int a,int b,int c) { int result=0; if (a==b){ System.out.println("a==b"); return 0;//Termination procedure } if (a>b){ result=a; } if (a<b){ result=b; } return result; } //Specific size public static int max(int a,double b) { int result=0; if (a==b){ System.out.println("a==b"); return 0;//Termination procedure } if (a>b){ result=a; } if (a<b){ result=(int)b; } return result; } }
4, Command line parameters
- Sometimes you want to pass a message to a program when it runs. This is achieved by passing command-line arguments to the main() function.
package method; public class Demo03 { public static void main(String[] args) { for (int i = 0; i <args.length ; i++) { System.out.println("args["+i+"]:"+args[i]); } } }
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG spagzumv-1635595853517) (C: \ users \ Wangcheng \ desktop \ markdown learning \ picture \ command transfer parameter. png)]
5, Variable parameters
- Starting with JDK 1.5, Java supports passing variable parameters of the same type to a method.
- In the method declaration, add an ellipsis (...) after specifying the 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.
package method; public class Demo04 { public static void main(String[] args) { //Calling methods with variable parameters printMax(34,3,3,2,56.05); printMax(new double[]{1,2,3}); } 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 = 0; i <numbers.length; i++) { if (numbers[i]>result){ result=numbers[i]; } } System.out.println("The max value is"+result); } }
6, Recursion
-
Method A calls method B, which is easy for us to understand!
-
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 small-scale problem similar to the original problem to solve. The recursive strategy only needs a small number of programs to seemingly find the multiple repeated calculations required in the solution process, 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.
Recursion is suitable for small calculations. If too many calculations are carried out, the memory will crash and crash.
package method; public class Demo05 { //recursive thinking /* Boundary conditions: boundary Previous stage: Return phase n*(n-1) */ public static void main(String[] args) { System.out.println(f(10)); } public static int f(int n) { if (n==1){ return 1; }else { return n*f(n-1); } } }