Fundamentals of Java methods
So what is the method?
public class MethodTest01{ public static void main(String[] args){ // Requirement 1: write a program to calculate the sum of 10 and 20 and output the results int a = 10; int b = 20; int c = a + b; System.out.println(a + "+" + b + "=" + c); // Demand 2: sum of 666 and 888, and output the results int a = 666; int b = 888; int c = a + b; System.out.println(a + "+" + b + "=" + c); /* The above requirement codes are similar. In essence, they are based on one requirement with the same function But the data of each participation is different * There should be a mechanism in Java: - The code of a certain function only needs to be used once - For the interaction between functions, you only need to input specific data to get the results Such code can be reused to improve code reusability This method is called [call / invoke] */ // [call / invoke] sumInt method MethodTest.sumInt(10, 20); MethodTest.sumInt(666, 888); // The effect is the same as the previous code } // Define a method separately [do not explain the method syntax temporarily] // Function: complete the calculation of the sum of two int type data and output the results public static void sumInt(int a, int b){ int c = a + b; System.out.println(a + "+" + b + "=" + c); } }
English word for Method: Method
The method is called [Function / Function] in C language
Java method: a collection of statements that perform a function together
- Methods are defined in the class body. Multiple methods can be defined in a class body
- Method cannot be defined in method body
- The method body consists of Java statements
- The method is an orderly combination of solving a certain kind of problems [top-down]
- Methods are created in the program and referenced elsewhere
- Only when it is called by / invoke can it be executed
Syntax of method
The contents in brackets are optional
[Modifier list] Return value type method name(parameter list){ Method body [return Return value;] }
[return return value;] If not, the return value type is void
As long as it is data, there are data types, so the return value and return value type must be consistent
Explanation of grammatical structure:
-
Modifier list
Optional, not necessary
Tells the compiler how to call the method and defines the access type of the method
At present, it is uniformly written as: public static [to be explained later]
The modifier list has static keyword calling format: class name Method name (argument list);
Argument list: the data you want to transfer according to the actual situation
-
return type
A method can complete a specific function. After the function is completed, most of them need to return the final execution result
The return value type needs to be determined by the programmer according to the actual method
The method may not have a return value. At this time, the return value type is void. At the same time, there can be no return value, but return can be written;
-
Method name
It is enough to meet the requirements of identifier. It is best to see the meaning of name
The method name uses the small hump naming method, and the first word is preferably a verb
-
Parameter list: contains formal parameters
The formal parameter is a local variable: int a; double b; float c; String args; ...
Number of formal parameters: 0 ~ N
Use between multiple formal parameters; separate
The data type of the formal parameter plays a decisive role in the formal parameter. The name of the formal parameter is only the name of the local variable
When the method [invokes / invoke], the actual data actually passed to the method is called [argument]
The formal parameter list and the actual parameter list need data type correspondence and quantity correspondence
-
Method body
The method body must be enclosed by braces
The method body contains specific Java statements in logical order [top-down]
-
As long as the statement with the return keyword is executed, the method in which the return is located is forced to end
Hypothesis: you imagine you want to run a factory
- The parameter list is the raw material you provide [but you don't know the specific value yet]
- The return value type is what you want to generate [specify a type and model first]
- The return value is the physical output of the factory. Of course, it may not be shipped
- The method name is the name of the factory you want to run
// Public means public // Class represents the definition class // MethodTest02 a class name public class MethodTest02{ // Here class body // Java statements cannot be written directly in the class body, except for explanatory variables // Here is a method // Public means public // Static stands for static // void indicates that no data is returned after the method is executed // Main a special method name [main method] // (String[] args): formal parameter list, where String [] is a data type [String], and args is the variable name of a local variable // The compilation of the main method is fixed, because it is specified by SUN company and represents the entry of the program public static void main(String[] args){ // Here is the method body int num; // Call the calculation 10 and 20 of the method sumInt() to sum and output the result, and return the sum num = sumInt(10, 20); // (10, 20) is a list of arguments System.out.println("The sum returned from the method: " + num); // Call again num = sumInt(num, 10); // (num, 10) is a list of arguments } // Here is a custom method // Method function: calculate the sum of two int data, output the calculation result and return the sum // Planning is needed before writing a method // Modifier type: public static // Method name: sum // Formal parameter list: (int a, int b) two int data are required // Return value type: int return value is the sum of two int data, and the calculated sum is still int // Method body: mainly sum, output calculation results and return sum public static int sumInt(int a, int b){ // (int a, int b) is a list of formal parameters int c = a + b; System.out.println("Call method sumInt Output of: " + c); return c; } }