[1] What is the method?
A method is a piece of code used to complete a specific function, similar to the function of other languages.
Method is used to define the behavior characteristics and functional implementation of the class or an instance of the class. Methods are abstractions of the behavioral characteristics of classes and objects. Methods are very similar to procedure oriented functions. In process oriented, function is the most basic unit, and the whole program is composed of function calls. In object-oriented, the basic unit of the whole program is class, and the method is subordinate to class and object.
[2] Method declaration format:
[modifier 1, Modifier 2...] return value type method name (formal parameter list){
Java statements
}
[3] Method call method:
Object name Method name (argument list)
[4] Detailed description of the method
Formal parameter: used to receive incoming data when declaring a method.
Argument: the data actually passed to the method when the method is called.
Return value: the data returned to the calling environment after the method is executed.
Return value type: the data type of the return value agreed in advance. If there is no return value, it must be specified as void.
[5] Code:
public class TestMethod01{ /* 1.The method is to extract specific functions to form a code fragment, which is what we call the method 2.Methods and methods are parallel, so the methods we define cannot be written in the main method 3.Method definition format: = = "format: Modifier method return value type method name (formal parameter list){ Method body; return Add the return value of the method } 4.Method: improve the reusability of code */ //Definition of method: (write method) public static int add(int num1,int num2){ int sum = 0; sum += num1; sum += num2; return sum;//Returns the return value to the call of the method } public static void main(String[] args){ //10+20: //Method call: (using method) int num = add(10,20); System.out.println(num); /* int num1 = 10; int num2 = 20; int sum = 0; sum += num1; sum += num2; System.out.println(sum); */ //30+90: int sum = add(30,90); System.out.println(sum); /* int num3 = 30; int num4 = 90; int sum1 = 0; sum1 += num3; sum1 += num4; System.out.println(sum1); */ //50+48: System.out.println(add(50,48)); } }
[6] Summary:
1. The method is to extract specific functions to form a code fragment, which is what we call the method
2. Methods and methods are parallel, so the methods we define cannot be written to the main method
3. Definition of method – format:
Modifier method return value type method name (formal parameter list){
Method body;
return method returns a value;
}
4. Function of method: improve the reusability of code
5. Format of summary method definition:
1) Modifier: temporarily use public static
2) Method return value type: the data type corresponding to the return value of the method
Data type: it can be basic data type (byte,short,int,long,float,double,char,boolean) or reference data type
3) Method name: see the name and meaning, the initial letter is lowercase, and the rest follow the hump naming, eg: addNum, which is generally named in English as far as possible
4) Formal parameter list: formal parameters required for method definition: int num1, int num2 -- >, which is equivalent to telling the method caller that several parameters need to be passed in and the type of parameters need to be passed in
Actual parameters: specific parameters passed in during method call: 10,20 -- > passed in according to the needs of formal parameters
5) Method body: specific business logic code
6) return method returns a value;
If the method has a return value: return + method return value, return the return value to the method call
If the method has no return value: return can be omitted, and the return value type of the method is void
public class TestMethod02{ //Definition of method: (write method) public static void add(int num1,int num2){ int sum = 0; sum += num1; sum += num2; System.out.println(sum); //return; } public static void main(String[] args){ //10+20: //Method call: (using method) add(10,20); //30+90: add(30,90); //50+48: //System.out.println(add(50,48));// report errors } }
When is there a return value and when is there no return value? Looking at mood – looking at needs
6. What should be paid attention to in the definition of method?
1) How to write the formal parameter list: define several parameters and what types they are - "uncertain factors" we will use as the formal parameters of the method
2) Whether the method needs a return value, and if so, what is the type of the return value
7. What should I pay attention to when calling methods?
1) How to pass in actual parameters: how many parameters are passed in and what type of parameters are passed in
2) Whether the method has a return value to accept