What is a method (function)
: A method is an abstraction of the behavioral characteristics of a class or object, also known as a function
(Function, mathematical concept y=f(x) This f is equivalent to some kind of processing, f is a function, x is input, y is output, and x gets y after some kind of processing)
: In java, all methods must be defined in the class. And the method cannot be nested, the relationship between the methods is a level relationship, and the method cannot exist independently
: method = method header (method signature, instruction manual) + method body {}
: The method must be called before it can be executed
1. Method declaration
modifier return value type method name(parameter type 1 formal parameter 1,parameter type 2 formal parameter 2,...){ code; return return value; }
Formal parameters: Variables used to receive external incoming data when the method is called
parameter type: the data type of the formal parameter
Return value: After the method execution is completed, the data returned to the caller
Actual parameter: When calling the method, the data actually passed to the function parameter
Modifier: public static
Second, the method call
The method can only be executed if it is called
If the method itself is called in the method, it is called recursive call (serial call)
Method call statement:
Method name (actual parameter 1, actual parameter 2, ...);
return value type variable name = method call;
There are 4 combinations of methods:
No parameter, no return // The parameterless method must be called with ( ) test()
Return without parameter
With parameters but without return //When there is no return value type, use void for the return value type
There are parameters and returns
Note: The method without return cannot receive the return value, pure call;
When there is no return value, the return statement in the last line can be omitted;
When there is no return value, the return value type is represented by the keyword void
Example:
public class Method1 { //modifier return type method name (parameter type formal parameter) public static void test1(){ //No parameters and no return When there is no return value type, use void for the return value type System.out.println("test1"); return; //If there is no return value, return can be omitted } public static int test2(){ //no parameters return System.out.println("test2:"); return 1; } public static void test3(int a){ //There are parameters but no return System.out.println("test3"); } public static int test4(int a){ //There are parameters and returns System.out.println("test4:"); return a; } //method call public static void main(String[] args) { test1(); //No parameter and no return When calling a method without parameters, ( ) must be included System.out.println("The return value is"+test2()); //no parameters return test3(30);//There are parameters but no return int t=test4(400); System.out.println("The return value is"+t);//There are parameters and returns } }
3. The execution process of the method
1) Parameters are passed, and actual parameters are assigned to formal parameters (precise matching is preferred, and then compatibility matching) 2) The caller method does context saving and hangs 3) The called method starts executing 4) The called method ends and returns to the calling location of the calling method 4) The caller method does scene recovery and continues to execute the following code
Example:
public class Method2 { public static double test(double a){ //no parameter no return double b=2*a; System.out.println("test already executed"); return b; } public static void main(String[] args) { System.out.println("main start"); double a=test(5); System.out.println(a); System.out.println("main Finish"); } }
result:
main start test already executed 10.0 main Finish
Notice :
The process of passing actual parameters to formal parameters is essentially a process of assignment. A data type with a small capacity can be assigned to a data type with a large capacity.
Capacity order byte,short,char >int > long > float >double
In the above example, the int type is assigned to the double type, and no error will be reported. Otherwise, a strong escape character must be used.
4. Method overloading
In the same class, multiple methods with the same name are allowed, but the parameters must be different.
Overload Features:
1. In the same class, the method name is the same, but the parameters are different
2. Different parameters are reflected in: the number of parameters is different, the type of parameters is different, and the order of parameter types is different
3. Overloading has nothing to do with the return value type
public class MyMath{ //to sum public static int add(int a ,int b){ return a+b; } //Overloading with different parameter types public static double add(double a ,int b){ return a+b; } //Order of overloaded parameter types is different public static double add(double a ,int b){ return a+b; } //Overloading with different number of parameters public static int add(int a ,int b,int c){ return a+b+c } }