catalogue
2, Method invocation mechanism
3, Parameter transmission mechanism of method
1, Method
The method is to extract a function and define the code in a brace to form a separate function. When you need this function, you can call it. This not only realizes the reusability of the code, but also solves the phenomenon of code redundancy.
Modifier return value type method name (parameter list){ code... return Return value; }
Definition format interpretation:
- Modifier: used to control the scope of method use, including permission modifier, static modifier and final modifier.
- Return value type: the data type representing the result of the method operation. After the method is executed, the result is returned to the caller. A method can only have one return value. void indicates that there is no return value.
- Method name: name the method we define, which meets the specification of identifier and is used to call the method.
- Parameter list: unknown data of the method during operation, which is passed when the caller calls the method. There can be no parameters or multiple parameters separated by commas. The parameter type can be any type, and the basic type and reference type can be used. When this method is called, the parameters passed in should be parameters of the same or compatible type as the parameter list order.
- Return: bring the result of the method execution to the caller. After the method is executed to return, the overall method operation ends. If there is no return value, the statement is not necessary, but you can also write return. When the code is executed, it indicates that the method ends.
Methods must be defined in one class, but not in another. To define a method, the return value type and parameter list must be specified. The return value type must be the same as that returned by the return statement, otherwise the compilation fails. You cannot write code after return. Return means that the method ends. All subsequent code will never be executed. It is invalid code.
Parameters during method definition are called formal parameters, and parameters during method call are called arguments. The types of arguments and formal parameters must always be or compatible, and the number and order must be consistent.
Methods cannot be nested!!
Methods in the same class can be called directly. Cross class calls need to be called through object name, such as object name. Method name (parameter), Method calls are also related to access modifiers.
public class Method01 { public static void main(String[] args) { // Use of methods // After the method is defined, if it is not called, it will not be executed // Create the object first and then call it Person p1 = new Person(); p1.speak(); // The method is called here p1.cal01(); p1.cal02(1000); // If 1000 is passed in, n=1000 p1.cal02(5); // The method of an object can be called multiple times // Assign the int value returned by this method to the sum variable int sum = p1.getSum(10, 20); // If the transmission parameter is 10,20, then n = 10 and M = 20. int sum is used to receive the return value of the method System.out.println("getSum The return value obtained by the method is:" + sum); } } class Person { String name; int age; // Method (member method) // Output "I'm a good man" // Public: the presentation method is public // void: indicates that the method has no return value // speak: method name // (): parameter list // {}: the method body is the code to be executed public void speak() { System.out.println("I am a good man"); } // Calculation 1 +... + 100 public void cal01() { // Cycle int res = 0; for (int i = 1; i <= 100; i++) { res += i; } System.out.println("cal01 Calculation results:" + res); } // Calculate 1+...+n // (int n): formal parameter list, indicating that the current method can pass in an int value public void cal02(int n) { int res = 0; for (int i = 1; i <= n; i++) { res += i; } System.out.println("cal02 Calculation results:" + res); } // Calculates the sum of two numbers and returns // Int after public: indicates an int value returned to the caller after the method is executed // (int n, int m): there are two formal parameters, indicating that the current method can receive two parameters // return res;: Indicates that the value of res is returned to the caller public int getSum(int n, int m) { int res = m + n; return res; } }
2, Method invocation mechanism
(refer to code above)
- When the program executes the method, it will open up an independent space (stack space)
- When the method is completed or executed to the return statement, it will return
- Return to where you called
- After returning, continue to execute the code after the method
- When the main method is executed, the program exits
3, Parameter transmission mechanism of method
public class MethodParameter01 { //Write a main method public static void main(String[] args) { int a = 10; int b = 20; //Create AA object name obj AA obj = new AA(); obj.swap(a, b);//Call swap System.out.println("\nmain method a=" + a + " b=" + b);//a=10 b=20 } } class AA { public void swap(int a, int b) { System.out.println("\na and b Value before exchange\na=" + a + "\tb=" + b);//a=10 b=20 // Completed the exchange of a and b int tmp = a; a = b; b = tmp; System.out.println("\na and b Value after exchange\na=" + a + "\tb=" + b);//a=20 b=10 } }
The basic data type passes a value (value copy), and any change in the formal parameter does not affect the argument
When executing obj.swap(a, b); In the swap method, the values of parameters a and B of the method are exchanged, but the values of a and B in the main method are not exchanged, so the output is as follows:
public class MethodParameter02 { //Write a main method public static void main(String[] args) { B b = new B(); int[] arr = {1, 2, 3}; b.test(arr); //Call method System.out.println(" main of arr array "); //Traversal array for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } System.out.println(); } } class B { public void test(int[] arr) { arr[0] = 200; //Modify element // Traversal array System.out.println(" test100 of arr array "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + "\t"); } System.out.println(); } }
The reference type passes the address (the value is also passed, but the value is the address). You can affect the argument through formal parameters!