1. Pay attention to the implementation of the method
① Methods cannot be nested (methods are level)
② void indicates that there is no return value. You can omit return or write return separately without data
/* Definition and invocation of methods with return values Define system: public static Data type method name (parameter){ return data } Call format: 1.Method name (parameter); 2.Data type variable name = method name (parameter); */ public class MethodDemo5 { public static void main(String[] args){ //1. Method name (parameter) //isEvenNumber(10); //true; //2. Data type variable name = method name (parameter); boolean flag = isEvenNumber(10); //boolean flag = true; System.out.println(flag); } //Requirements: public static boolean isEvenNumber(int number){ if(number % 2 == 0){ return true; }else{ return false; } } }
function:
true
1.2) general format of method
Format:
public static return value type method name (parameter){
Method body;
return data;
}
When defining a method, two things should be clear:
① Specifying the return value type is mainly to specify whether data will be returned after the method operation. If yes, write void; if yes, write the corresponding data type
② Specify parameters: mainly specify the type and quantity of parameters
When calling a method:
① Method of void type can be called directly
② Methods of non void type. It is recommended to receive calls with variables
*Note:
①public static Modifier
② Return value type: the data type of data returned after method operation
After the method operation is completed, no data is returned. void is written here, and return is generally not written in the method body
③ Method name: The identifier used when calling the method
④ Parameters: It consists of data type and variable name, and multiple parameters are separated by commas
⑤ Method body: Code block to complete the function
⑥return: If the method operation is completed, there is a data return, which is used to return the data to the caller
2. Method overload
Overview: method overloading refers to the relationship between multiple methods defined in the same class. Multiple methods that meet the following conditions constitute overloading
Conditions: ① multiple methods are in the same class
② Multiple methods have the same method name
③ Multiple methods have different parameters, different types or different quantities
Features: ① overloading only defines the method and has nothing to do with method calling. The calling method refers to the standard format
② Overloads only identify the names and parameters of methods in the same class, regardless of the return value. In other words, it is not possible to determine whether two methods constitute overloads with each other through the return value
example:
① The method name is the same, and the parameters are the same ×
② Same method name, different parameter list name √
③ Same method name, different parameter list name √
④ The method name is the same, the parameter list name is different, but the class is different! ×
/* Method overloading Multiple methods in the same class Multiple methods have the same method name Multiple methods have different parameters, different types or different quantities *Note: ① it has nothing to do with the return value ②When calling, the Java virtual opportunity distinguishes the methods with the same name according to different parameters */ public class MethodDemo6 { public static void main(String[] args){ //Call method int result = sum(10,20); System.out.println(result); double result2 = sum(10.0,20.0); System.out.println(result2); int result3 = sum(10,20,30); System.out.println(result3); } //Requirement 1: the method of finding the sum of two int type data public static int sum(int a, int b){ return a + b; } //Requirement 2: the method of finding the sum of two double type data public static double sum(double a,double b){ return a + b; } //Requirement 3: method for finding the sum of three int type data public static int sum(int a, int b, int c){ return a + b +c; } }
function:
30 30.0 60
example:
// Requirement 1: the method of finding the sum of two int type data
public static int sum(int a, int b){
return a + b;
}
// Requirement 2: the method of finding the sum of two double type data
public static double sum(double a,double b){
return a + b;
}
// Requirement 3: method for finding the sum of three int type data
public static int sum(int a, int b, int c){
return a + b +c;
}
*Note: these three requirements are all in the same class. The method names are sum, requirement 1 and requirement 2. The data types are different, requirement 1 and requirement 3. The number of parameters is different, and the types of requirement 2 and requirement 3 are different. Therefore, they constitute method overloading.
2.2) method overload exercise
Requirements: use the idea of method overloading to design a method to compare whether two integers are the same, and be compatible with all integer types (byte, short,int,long)
Idea:
① Define the compare () method to compare whether two numbers are the same. Select two int parameters for the parameters
public static boolean compare(int a, int b){
return a == b;
}
② Define the corresponding overloaded method, change the corresponding parameter type, and change the parameter to two long parameters
public static boolean compare(long a,long b){
return a == b;
}
③ Define all overloaded methods, two byte type and two short type parameters
public static boolean caompare(byte a , byte b){
// code snippet
}
public static boolean compare(short a, short b){
// code snippet
}
④ Complete the method call and test the running results
public static void main(String[] args){
System.out.println(compare(10,20));
}
/* Requirements: use the idea of method overloading to design a method to compare whether two integers are the same and compatible with all integer types (byte, short, int, long) Idea: 1.Define the compare () method to compare whether two numbers are the same. Select two int parameters for the parameters 2.Define the corresponding overloaded method, change the corresponding parameter type, and change the parameter to two long parameters 3.Define all overloaded methods, two byte type and two short type parameters 4.Complete the method call and test the running results */ public class MethodTest3 { public static void main(String[] args) { //Call method System.out.println(compare(10,20)); System.out.println(compare((byte)10,(byte)20)); System.out.println(compare((short)10,(short)20)); System.out.println(compare((long)10,(long)20)); } //int public static boolean compare(int a, int b){ System.out.println("int"); return a == b; } //byte public static boolean compare(byte a , byte b){ System.out.println("byte"); return a == b; } //short public static boolean compare(short a, short b){ System.out.println("short"); return a == b; } //long static boolean compare(long a, long b){ System.out.println("long"); return a == b; } }
function:
int false byte false short false long false
3. Method parameter transfer (basic type)
For the parameters of basic tree fern type, the change of formal parameters does not affect the value of actual parameters
public class ArgsDemo01 { public static void main(String[] atgs){ int number = 100; System.out.println("call change Before method:" + number); change(number); System.out.println("call change After the method:" + number); } public static void change(int number){ number = 200; } }
function:
call change Before method: 100 call change After method: 100
3.2) method parameter transfer (reference type)
public class ArgeDemo02 { public static void main(String[] args){ int[] arr = {10,20,30}; System.out.println("call change Before method:" + arr[1]); change(arr); System.out.println("call change After the method:" + arr[1]); } public static void change(int[] arr){ arr[1] = 200; } }
function:
call change Before method: 20 call change After method: 200