This article has been included: gitee.com/mydb/interv…
Method overloading is defined when multiple methods with the same name are defined in the same class, but each method has a different parameter type or number of parameters. For example, the following four methods can be called method overloading, as shown in the following code:
public class OverloadExample {
public void method(a) {
// doSomething
}
public void method(String name) {
// doSomething
}
public void method(Integer id) {
// doSomething
}
public void method(Integer id, String name) {
// doSomething}}Copy the code
Why do different return types not count as method overloading?
To answer this question, we need to know a little bit about method signatures. Method signatures are created by:Method name + parameter type + Number of parametersThat unique value is the method signature,This method signature is how the JVM (Java Virtual Machine) decides which method to call. We can see from the method signature of rules, the method’s return type is not part of the method signature, so when there’s multiple methods in the same class name and parameters are the same, but different methods, the return value type JVM can’t through the method signature to determine exactly which method to invoke, as shown in the figure below:So why can’t return types be part of method signatures? The reason is simple: if a method’s return type is part of the method signature, the JVM can’t tell which method to call when a programmer writes code to call an “overloaded” method, as shown in the following code:
public class OverloadExample {
public static void main(String[] args) {
OverloadExample example = new OverloadExample();
example.method("Leilei"); // Which method should the JVM call?
}
public int method(String name) {
// doSomething
return Awesome!;
}
public String method(String name) {
// doSomething
return "Let's talk about programming."; }}Copy the code
In this case, the JVM cannot infer which method to call, so the method return type cannot be part of the method signature.
Usage scenarios for method overloading
The classic use scenario for method overloading is the valueOf method of String. ValueOf method overloading has nine implementations, as shown in the following figure:It can convert arrays, objects, and underlying data types to string types.
Method overload matching principle
Method overloads are called in a sequential order, as in the following code:
public class OverloadExample {
public static void main(String[] args) {
OverloadExample example = new OverloadExample();
example.method(12);
}
public void method(int num) {
System.out.println("Call int method");
}
public void method(long num) {
System.out.println("Call the long method");
}
public void method(Integer num) {
System.out.println("Call the Integer method");
}
public void method(Object num) {
System.out.println("Call Object method");
}
public void method(int. num) { // This parameter is optional
System.out.println("Call the int... Methods"); }}Copy the code
Which method does the program call when a method overload occurs? The execution results of the above programs are as follows:Therefore, we can draw the following conclusions.
Matching principle 1: Exact type matching
Method overloading takes precedence over calling methods that are exactly the same as method parameter types. This is the first-priority matching principle: exact type matching.
Matching principle 2: Base types are automatically converted to larger base types
So let’s get rid of the exact matching method, and let’s see what’s the second matching order? The implementation code is as follows:
public class OverloadExample {
public static void main(String[] args) {
OverloadExample example = new OverloadExample();
example.method(12);
}
public void method(long num) {
System.out.println("Call the long method");
}
public void method(Integer num) {
System.out.println("Call the Integer method");
}
public void method(Object num) {
System.out.println("Call Object method");
}
public void method(int. num) { // This parameter is optional
System.out.println("Call the int... Methods"); }}Copy the code
The execution result of the above program is as follows:Therefore, we can draw the conclusion that:If it is a primitive data type, then the second matching rule for method overload calls is to automatically convert to the larger primitive data type.
Matching principle 3: Automatic packing and unpacking match
Then delete the long method in the second matching principle, and the code is as follows:
public class OverloadExample {
public static void main(String[] args) {
OverloadExample example = new OverloadExample();
example.method(12);
}
public void method(Integer num) {
System.out.println("Call the Integer method");
}
public void method(Object num) {
System.out.println("Call Object method");
}
public void method(int. num) { // This parameter is optional
System.out.println("Call the int... Methods"); }}Copy the code
The execution result of the above program is as follows:As can be seen from the above execution results,The third matching rule for method overloading is to match auto-boxing or unboxing data types.
Matching rule 4: Match the packets upward based on the inheritance line
At this point, the Integer method in the third matching principle is removed, and the remaining code is as follows:
public class OverloadExample {
public static void main(String[] args) {
OverloadExample example = new OverloadExample();
example.method(12);
}
public void method(Object num) {
System.out.println("Call Object method");
}
public void method(int. num) { // This parameter is optional
System.out.println("Call the int... Methods"); }}Copy the code
The execution result of the above program is as follows:As can be seen from the above execution results,The fourth matching rule for method overloading is to match method calls of the parent class up.
Matching principle 5: Variable parameter matching
The only optional argument left to remove the method from the code is as follows:
public class OverloadExample {
public static void main(String[] args) {
OverloadExample example = new OverloadExample();
example.method(12);
}
public void method(int. num) { // This parameter is optional
System.out.println("Call the int... Methods"); }}Copy the code
The execution result of the above program is as follows:As can be seen from the above execution results,The fifth matching rule for method overloading is to match optional arguments.
conclusion
Method overloading occurs when multiple methods with the same name are defined in the same class but each method has a different parameter type or number of parameters. A typical use case for method overloading is the valueOf method in String, which has nine implementations. The method return type cannot be used as a basis for method overloading because it is not part of the method signature. There are five matching principles for method overloading: precise matching, automatic conversion of basic type to larger basic type matching, automatic loading/unpacking matching, upward matching according to inheritance route, and variable parameter matching.
Reference: Code Out Efficiently
Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.
Public account: Java Chinese Community