What is the 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
Example:
package com.wmwx.method; public class Demo01 { //main method public static void main(String[] args) { int sum = add(1, 2); //Call the add method System.out.println(sum); } //The add method implements the addition function public static int add(int a, int b){ return a+b; } }
Advantages of the method
Methods in Java have the following advantages:
- Make the program shorter and clearer.
- It is conducive to program maintenance.
- It can improve the efficiency of program development.
- It improves the reusability of code.
Design principle of method
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
The basic syntax is:
Modifier return value type method name(Parameter type parameter name){ ... Method body ... return Return value; }
Java method contains a method header and a method body, which is similar to functions in other languages. It is a code fragment used to complete specific functions. The following are all components of a method:
- Modifier: optional, tells the compiler how to call the method, and defines the access type of the method.
- Return value type: a method may have a return value, and the data type of the return value is the return value type of the method. Some methods perform the required operation but do not return a value. In this case, the return value type of the method uses 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: a parameter is like a placeholder, also known as a formal parameter. When a method is called, it passes a value to the parameter, which is called an argument. Parameter list refers to the parameter type, order and number of parameters of a method. Parameters are optional, and a method can contain no parameters.
- Method body: the method body contains specific statements that define the function of the method.
Method call
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.
- If the return value of the method is void, the method call must be a statement.
Example:
package com.wmwx.method; public class Demo02 { public static void main(String[] args) { int maxNum = max(10, 20); //Call the max method with the return value as the value assigned to the variable maxNum System.out.println(maxNum); } //Define max method with return value public static int max(int num1, int num2){ int result = 0; if (num1==num2){ System.out.println("The two numbers are equal!"); return num1; } if (num1>num2){ result = num1; }else{ result = num2; } return result; } }
Method overload
Overloading: if multiple methods of a class have the same method name but different parameter lists, the Java compiler will judge which method should be called according to the method signature.
Rules for method overloading:
- Method names must be the same.
- The parameter list must be different (e.g. different number, different parameters, different order of parameters, etc.).
- The return types of methods can be the same or different
- Only different return types are not enough to overload methods
Example:
package com.wmwx.method; public class Demo03 { public static void main(String[] args) { int maxNum1 = max(10, 20); //Call the max method with integer type parameters System.out.println(maxNum1); double maxNum2 = max(10.0, 20.0); //Call the max method with floating-point type parameters System.out.println(maxNum2); } //max method with integer type parameters defined public static int max(int num1, int num2){ int result = 0; if (num1==num2){ System.out.println("The two numbers are equal!"); return num1; } if (num1>num2){ result = num1; }else{ result = num2; } return result; } //Overload: //max method with floating point type parameters defined public static double max(double num1, double num2){ double result = 0; if (num1==num2){ System.out.println("The two numbers are equal!"); return num1; } if (num1>num2){ result = num1; }else{ result = num2; } return result; } }
Command line parameters
Sometimes, if you want a program to pass messages to it at run time, you need to pass command-line parameters to the main method for implementation.
Example:
package com.wmwx.method; public class Demo04 { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println("args["+i+"]: "+args[i]); } } }
Variable parameter / indefinite parameter
Starting with Java 5, Java supports passing variable parameters of the same type (also known as indefinite parameters) to a method.
The declaration of variable parameters of the method is as follows:
typeName... parameterName //That is, in the method declaration, an ellipsis (...) is added after the specified parameter type
be careful:
- Only one variable parameter can be specified in a method.
- Variable argument must be the last argument of the method.
- Any ordinary parameter must be declared before the variable parameter.
Example:
package com.wmwx.method; public class Demo05 { public static void main(String[] args) { //Calling methods with variable parameters printMax(); printMax(12, 13.6, 22.5, 18.7, 16.0); printMax(new double[]{12, 13.6, 22.5, 18.7, 16.0}); } public static void printMax(double...numbers){ if (numbers.length == 0){ System.out.println("No incoming data!"); return; } double result = numbers[0]; //Find the maximum for (int i=1;i<numbers.length;i++){ if (numbers[i]>result){ result = numbers[i]; } } System.out.println("The maximum number is"+result+"!"); } }
recursion
Definition: the programming method of program call itself is called recursion.
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.
The recursive structure consists of two parts:
- Recursive header: when not to call its own method. If there is no recursive header, it will cause an endless loop.
- Recursive body: when to call its own method.
Example:
package com.wmwx.method; public class Demo06 { public static void main(String[] args) { System.out.println(factorial(5)); } //Factoring public static int factorial(int n){ if (n==1){ return 1; }else{ return n*factorial(n-1); //Recursive call } } }