1.1Method Overview
in Java , a method is a way to accomplish something or to implement a function. method Implementation, there are a number of statements that are used to accomplish some meaningful function -usually working with text, controlling input, or calculating values. we can implement (or call) the method in the program code by referencing the method name and the required parameters. Methods, which generally have a return value, are used as the result of the processing of the event.
1.2syntax format of the method
in the in Java, the specific syntax format for declaring a method is as follows:
modifier Returns a value type method name ( parameter type argument name 1, argument type parameter name 2,...). ){
EXECUTE statement
.........
return value ;
}
Modifiers: Methods have more modifiers, restrict access, have static modifier statics, and final modifier final , which will be introduced in the subsequent learning process .
Return value type: The data type used to qualify the method return value
Parameter type: Used to qualify the data type of the parameter passed in when calling the method
Parameter name: is a variable that is used to receive incoming data when the method is called
Return keyword: Used to end a method and return a value of the specified type of method
return Value: The value returned by the return statement, which is returned to the caller
It is important to note that the parameter name 1 in the method ,argument type parameter name 2, is called the parameter list, which describes the parameters that the method needs to receive when it is called, and if the method does not need to receive any parameters, The argument list is empty, meaning nothing is written in (). The return value of the method must be the return value type of the method declaration, and if there is no return value in the method, the return value type is declared void, at which point the return statement in the method can be omitted.
Public classMethodDemo01 { Public Static voidMain (string[] args) {intArea = Getarea (3, 5);//calling the Getarea methodSystem.out.println ("The area is" +Area ); } //The following defines a method for calculating rectangular area, receiving two parameters, where x is high and Y is wide Public Static intGetarea (intXinty) {inttemp = x * y;//use variable temp to remember the result of the operation returnTemp//returns the value of the variable temp }}
1.3Overloading of methods
Java allows you to define multiple methods with the same name in a class, but the type or number of parameters must be different, which is the overload of the method.
public static int Add (int x,int y) {logical}//Two integer addition
public static int Add (int x,int y,int z) {logical}//Three integer addition
public static int Add (double x,double y) {logical}//Two decimal additions
The overloads of a method are independent of the return value type, which has only two conditions, one is the same method name , the other is the number of arguments, or the parameter type is not the same .
overloaded method parameters must be different:
Different number of arguments, such as method (int x) and method (int x,int y)
parameter types differ, such as method (int x) differs from method (double x)
parameter order differs, such as method (int x,double y) differs from method (double x,int y)
overloading is only related to the method name and the parameter type, regardless of the return value . such as void method (int x) and int method (int y) are not methods overloads and cannot exist concurrently
overloading is independent of the specific variable identifier , such as method (int x) and approach (int y) are not methods overloaded and cannot exist simultaneously
1.4 parameter passing
when defining a method, a variable in the argument list, which we call the formal parameter ; when calling a method, the numeric value passed to the method, which we call the actual parameter
When the method is called, if the value passed in is the base data type (contains String type ), the change of formal parameters does not affect the actual parameters
When the method is called, if the value passed in is a reference data type ( Except for the string type), the change in formal parameters has an effect on the actual parameters
1.5 Summary
Precautions for using the method:
1, method does not call, does not execute itself
2, methods cannot be defined in methods, but methods can be called in methods
3, the method defines the position in the class, outside of the other methods
4, If the method does not have an explicit return value type, use the ' empty ' type, void indicates
5,void can only be used in the method return value type location and cannot be used as a normal data type
6, If the method return value type is void type, you can omit return;
Method Invocation Mode:
method invocation methods with a definite return value type: separate calls ; output calls ; Assignment Invocation
method invocation with no explicit return value type: called separately
method overloads: method overloads, in the same class, there are multiple methods with the same name, and their argument lists are different (The number of parameters list is different, the parameter list has different data types, the order of the parameter list is different ) .
method Overloading Features: regardless of the return value type of the method, regardless of the parameter name of the method, look only at the method name and the parameter list; method overloads are passed through the JVM to complete the invocation of a method of the same name, the parameter list determines which method is called.
Java Basic Syntax----method