First, the method
(1) Method: A block of code that completes a specific function.
Note: In many languages there is a definition of a function, whereas in Java, a function is called a method.
(2) Format:
Modifier returns a value type method name (parameter type argument name 1, argument type parameter Name 2 ...) {
Method body Statement;
return value;
}
Modifier: Use public static now.
Return value type: is the data type of the functional result
Method Name: It is a name that makes it easy for us to call this method.
Parameter type: is the data type of the parameter
Parameter name: is the variable
Parameter classification:
Arguments: Data that actually participates in the operation
Parameter: A variable defined on a method that receives an actual argument
Method Body Statement: Is the code block that completes the function
Return: End method
Return value: Is the result of the function, which is brought to the caller by return.
(3) Two clear:
Return value type: The data type of the result
Parameter list: number of parameters and corresponding data types
(4) Precautions for the method
A: Method does not call do not execute
B: The method is a peer relationship and cannot be nested defined
C: When the method is defined, the arguments are separated by the
D: The method does not pass the data type at the time of the call
E: If the method has an explicit return value type, a return statement must be returned.
Second, the method of overloading
In the same class, the method name is the same, and the argument list is different, regardless of the return value.
Description: Different parameter list (3 cases)
A: Different numbers
B: Different order
C: Different data types
Third, permission modifier
| modifier |
Current Class |
in the same package |
descendant class |
Other Packages |
public |
Y |
Y |
Y |
Y |
protected |
Y |
Y |
Y |
N |
default |
Y |
Y |
N |
N |
private |
Y |
N |
N |
N |
Description: Public modifier, visible to all classes
Protected (refers to the protected) current class, under the same package other classes and their subclasses can access
Default (default modifier, do not write) can only be accessed by the current class or other class under the same package
Private (privately) only current class access
Iv. implementation process of the method
V. Memory load of the method
classmethoddemo{ Public Static voidMain (string[] args) {intA = 3; intb = 4; intsum =Getsum (A, b); System.out.println ("Sum=" +sum); } Public Static intGetsum (intAintb) {returnA +b; }}
Memory
Description: First load the Main method loaded into the stack memory, and execute the code in the Main method, respectively, a variable to open space and storage 3, the b variable space to store 4. When the program executes to int sum = Getsum (A, B), the getsum function is loaded into the stack memory, and the A and B variable spaces are also opened in the stack area to which the getsum belongs , accepting the values in the Main method Getsum (A, b); the value passed. Then execute the code in the Getsum function, and when the getsum function finishes, the function will stack up (the stack). The program returns to the main method's call statement int sum = Getsum (A, B), and assigns the Getsum method to the sum variable after execution of the returned result, and the program continues to execute down. Prints the value of sum.
Java Learning Notes---methods