Basic definition and use of method
1. Concept of method
Method: method, also known as function, is an orderly combination of codes used to solve a class of problems. It is a functional module. (personal popular understanding is like a data processing tool. Put the original data into it for processing, and then get the results I want.)
2. Basic definition of method
//Syntax: [Access modifier ] [Common modifier] Return value data type method name(parameter list ){ Method body; }
Composition of method | |
---|---|
Access modifier | public, protected, friendly (default), private |
Common modifier | [static] final synchronized |
Return value data type | 1. No return value void (Void) 2 There is a return value type (any data type can be used) |
Method name | Specification: lowercase hump naming identifier |
parameter list | 1. No reference 2 There are parameters (1 or more or variable parameters) to be used and separated |
Method body | Logic code |
Combined with the return value and formal parameters, methods can be roughly divided into four types: no parameter no return value method, parameter no return value method, no parameter with return value method and parameter with return value method.
be careful:
1. The access modifier can also be omitted. If omitted, it is the default access modifier friendly
2. Because methods in Java can be overloaded, when the method names are the same, the program distinguishes the methods with the same name through different formal parameter lists.
3. The method is in the java file. The method and method are at the same level.
3. Precautions for chuangjie method
- If the return type of the method is void, the return value cannot be used in the method!
- Method can only return one value at most, and cannot return multiple values.
- The type of the return value of the method must be compatible. For example, if the return value type is int, you cannot return a String value.
- When there are multiple method parameters, the multiple parameters are separated by commas.
- Method parameters can be basic data types, such as int, double, etc., or reference data types, such as String, array, etc.
4. Precautions when calling methods
- When calling a method with parameters, you must ensure that the number, type and order of arguments correspond to the formal parameters one by one.
- When calling a method, the argument does not need to specify a data type
4. Classification of methods
(1) Parameterless return value method
The return type of the method is void, which means the return value is empty and the parameter list is empty.
/** * @Title: Chapter 6 * @Project name example 1 * @Project Description: create a parameter free and return value free method of holleWorld(), and output "Hello, world!" * @Created by yaojiawen * @Creation time: 2021 / 4 / 1619:31 */ public class LiTi1 { //Create method holleWorld static void holleWorld() { System.out.println("Hello, world!"); } public static void main(String[] args) { holleWorld(); } }
(2) Method with parameter and no return value
The return type of the method is void, which means the return value is empty, and there are one or more parameters in the parameter list.
import java.util.Scanner; /** * @Title: Chapter 6 * @Project name example 2 * @After the two methods are created, the maximum number of input parameters of a (max) and B (max) will be returned, and then the maximum number of input parameters of B will be returned. * @Created by yaojiawen * @Created at 2021 / 4 / 1714:58 */ public class LiTi2 { static void max(int a, int b) { int max = a > b ? a : b; System.out.println("The maximum value is:" + max); } public static void main(String[] args) { Scanner input = new Scanner(System.in); int num1 = input.nextInt(); int num2 = input.nextInt(); max(num1, num2); } }
(3) Nonparametric return value method
The return value type of the method is the return value type of retuen, and the parameter list is empty.
import java.util.Scanner; /** * @Title: Chapter 6 * @Project name example 3 * @Project Description: create a parameterless return value method of pingJuenZhi(), input three numbers after calling, and then return an average value * @Created by yaojiawen * @Created at 2021 / 4 / 1715:47 */ public class LiTi3 { static double pingJuenZhi() { Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double pingJuen = (a + b + c) / 3; return pingJuen; } public static void main(String[] args) { System.out.println("The average value is:" + pingJuenZhi()); } }
(4) Method with parameter and return value
The return value type of the method is the return value type of retuen, and there are one or more parameters in the parameter list.
/** * @Title: Chapter 6 * @Project name example 4 * @Project Description: create a test(String name, String password) user name and password verification method. If it is correct, it returns true and if it is wrong, it returns false. * @Created by yaojiawen * @Created at 2021 / 4 / 1715:57 */ public class LiTi4 { //User name and password judgment method test static boolean test(String name, String password) { String yongHu = "admin"; String miMa = "123456"; boolean panduan = false; if (yongHu.equals(name) && miMa.equals(password)) panduan = true; return panduan; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter user name:"); String name = input.next(); System.out.println("Input password:"); String password = input.next(); if (test(name, password)) System.out.println("Correct input!"); else System.out.println("Input error!"); } }
5.java variable length parameter
When the number of parameters of the same type in a certain part is uncertain, variable length parameters can be used to pass uncertain parameters in the method definition. (it feels a bit like an array)
Use syntax
data type... Variable name
Precautions during use
- In the method with variable length parameters, the parameters can be used as an array. For example, use foreach loop to output all parameter values.
- Each method can have at most one variable length parameter, and the position of the parameter is the last of the method.
- When calling, you can give any number of parameters or no parameters.
- Method overloading with variable length parameters should be avoided as far as possible.
- If a method with variable length parameters is overloaded, the method with a fixed number of parameters will be called first.
/** * @Title: Chapter 6 * @Project name example 4 * @Project Description: create a method to connect the uncertain number of strings and return a new connected string. * @Created by yaojiawen * @Creation time: 2021 / 4 / 1717:01 */ public class LiTi5 { static String lianJei(String... a) { String shu = ""; for (String b : a) { shu += b + " "; } return shu; } public static void main(String[] args) { String a = "everybody"; String b = "Take off!"; System.out.println(lianJei(a, b)); } }
6. Overloading of methods
Method overloading: if the same class contains two or more methods with the same method name and different number, order or type of method parameters.
(simplified as) method overloading: in the same class, the method names are the same and the parameter types are different.
Note: when calling an overloaded method, Java will judge which overloaded method should be called according to the number and type of parameters, and the method with exactly matching parameters will be executed.
Basis for judging method overload:
- Must be in the same class.
- Same method name.
- The number, order, or type of method parameters are different.
- It has nothing to do with the modifier or return value of the method.
/** * @Title: Chapter 6 * @Project name example 4 * @Project Description: create a method to connect an uncertain number of strings and return a new connected string, and then overload this method. When there is only one parameter, the string connects "new day!" And output! * @Created by yaojiawen * @Created at 2021 / 4 / 1717:51 */ public class LITi6 { static String lianJei(String... a) { String shu = ""; for (String b : a) { shu += b + " "; } return shu; } static String lianJei(String a){ String shu="another day!"; return shu+=a; } public static void main(String[] args) { String a = "take off"; String b = "everybody!"; System.out.println(lianJei(a, b)); //This calls lianJei(String... a) System.out.println(lianJei(a)); //This calls lianJei(String a) } }