9. Method
9.1 what is a method?
The function code only needs to be entered once
To use this function, you only need to pass specific data to this function
After this function is completed, a final result is returned
Such code can be reused to improve code reusability
Methods are defined in a class body. Multiple methods can be defined in a class body. There is no order in which methods are written, and they can be written at will.
The method body is composed of java statements, and the code in the method body is executed at one time in top-down order.
Method cannot be defined in method body.
Method is a code fragment, and this code fragment can complete a specified function and can be reused.
9.2 definition of method
9.2.1 definition of methods with specific return value types
[Modifier list] Return value type method name (formal parameter list){ Method body; }
Explain the above grammatical structure:
Modifier list:
Public: the access permission is large enough to be public
Static: static modifier (in object-oriented)
Return value type:
Is the data type (the basic data type currently used...)
Method name:
See the name to know the meaning (small hump naming method)
Formal parameter list:
The data type of the formal parameter plays a decisive role in the formal parameter. The name of the formal parameter is the name of the local variable
When a method is called, the real data actually passed to the method is called an argument
Argument and formal parameter lists must have the same quantity and the same type
Method body:
The method body must be enclosed by braces. Your code in the method body is executed in order from top to bottom.
And the method is composed of java statements, and each java statement is marked with " ending.
9.2.2 definition of methods with specific return value types:
[Modifier list] void Method name (formal parameter list){ Method body; }
9.2.3 defining a method requires two clear points:
1) Explicit return value type: there is no specific return value type
2) Specify the parameter type and the number of parameters
9.3 method call
Class name.Method name (argument list);//This is a java statement that calls a method of a class and passes such an argument
Methods are executed only when they are called. They will not be executed if they are defined without calling.
Method has static in its modifier list
When a method is called, the number of arguments and formal parameters must be the same and the data type must be the same
When types are different, corresponding automatic type conversion is required
When calling the static modified method in this class, the class name can be omitted (only in this case, it can be omitted)
9.4 allocation and change of memory in JVM during method execution
9.4.1. Memory allocation
The method is only defined and will not be executed without calling, and the memory space of "run to" will not be allocated to the method in the JVM. Only when this method is called will the memory space of this method be allocated dynamically.
9.4.2. Memory allocation
There are three main memory spaces in the JVM memory partition:
Method area memory
Heap memory
Stack memory
9.4.3 stack data structure:
Data structure reflects the storage form of data
Common data structures:
Array, queue, stack, linked list, binary tree, hash table
Concept:
stack is a data structure
The stack frame always points to the top element of the stack
The top element of the stack is active and the other elements are static
Terminology:
Push / stack / push
pop up / out / pop
characteristic:
In and out
Last in first out
9.4.4 where does the code fragment exist when the method is executed? Where is the memory allocated when the method is executed?
Method code snippets belong to Class. The bytecode file is placed in the method area when the class is loaded. Therefore, among the three main memory spaces in the JVM, the method area has data first. Stored code snippets.
Although there is only one code fragment in the method area memory, it can be called repeatedly. Each time this method is called, a separate activity place needs to be allocated to the method and allocated in the stack memory. [memory space in stack memory to allocate method operation]
The method will allocate memory space to the method at the moment of calling, and the stack pressing (allocating memory to the method) action will occur in the stack. After the method is executed, all the memory space allocated to the method will be released, and the stack bouncing (releasing the memory space of the method) action will occur.
Local variables are declared in the method body, and the memory of local variables is allocated in the stack during the run-time.
9.5 overloading of methods
When overloading is not used:
package heavy load; public class heavy load { public static void main(String[] args) { int a = sumInt(1,2); System.out.println(a); double b = sumDouble(1.0,2.0); System.out.println(b); long c = sumLong(1L,2L); System.out.println(c); } //Define a method to calculate the sum of int types public static int sumInt(int a, int b){ return a+b; } //Define a method to calculate the summation of double type public static double sumDouble(double a, double b){ return a+b; } //Define a method to calculate the sum of long types public static long sumLong(long a, long b){ return a+b; } }
Although the functions of the above contents and methods are different, the functions are similar, which are summation. In the following programs, methods with similar functions have three asynchronous names respectively. It is inconvenient for programmers to call methods. Programmers need to remember more methods to complete the call. [inconvenient and unsightly]
Overload: Although the functions are different, when the "functions are similar", programmers can use these methods as if they were using the same method. In this way, programmers can write code more conveniently in the future, do not need to remember more method names, and the code will be very beautiful.
After using heavy load:
Heavy load 2: public class Overload 2 { public static void main(String[] args) { a.b(true); a.b(2.0); a.b(null); } } class a{ public static void b(byte c){ System.out.println(c); } public static void b(short c){ System.out.println(c); } public static void b(char c){ System.out.println(c); } public static void b(boolean c){ System.out.println(c); } public static void b(long c){ System.out.println(c); } public static void b(double c){ System.out.println(c); } public static void b(String c){ System.out.println(c); } public static void b(int c){ System.out.println(c); } }
**
to be continued...
**