1. Method overloading
The method name is the same, but the parameters are different, this is overload.
The so-called different parameters have two main points: the first is that the number of parameters is different, and the second is that the types of parameters are different. As long as one of these two aspects is different, it can constitute an overload of the method.
package cn.galc.test;public class TestOverLoad { void max(int a, int b) { System.out.println(a > b ? a : b); } /* * int max(int a, int b) { * return a > b ? a : b; * } */ void max(float a, float b) { System.out.println(a > b ? a : b); }}Here, the two max methods with void modifiers have the same name, but their parameter types are different, so they can constitute overloading. The int max(int a , int b) method and the void max(int a, int b) method do not constitute overloading. Now they are two methods with duplicate names. Declaring two methods with duplicate names in a class is not allowed, and there will be an error in compilation. The method name is the same and the parameter type is the same. Only the return value is different. This does not constitute an overload, because the method can be called in this way. It can not be used when calling a method. Therefore, when calling these two methods, the integer number is passed in. Since both methods have the same name and the same parameter type, the compiler cannot distinguish which method is to be called. The reason for the deep level of overloading: As long as the compiler of these two methods can distinguish them and know which one to be called when calling, there will be no confusion, these two methods constitute overloading.
Let’s look at the following two methods:
int max(int a, int b) { System.out.println("The int max(int a, int b) method called"); return a > b ? a : b;} int max(short a, short b) { System.out.println("The int max(short a, short b) method called"); return a > b ? a : b;} These two methods can also constitute overloading, because the compiler will treat it as an int type as soon as he sees an integer. So when passing an integer in, the compiler first calls the method max(int a , int b). If you want to call the method max(short a , short b), you have to write it in the main method like this:
public static void main(String[] args) { TestOverLoad t= new TestOverLoad(); t.max(3,4); //The method max(int a , int b) is called short a = 3; short b = 4; t.max(a, b); //The method max(short a , short b) is called here. }2. Overloading of construction methods
Like ordinary methods, constructors can also be overloaded
package cn.galc.test;public class Person { int id; int age; /** * Constructor*/ public Person() { id=0; age=20; } /** * Constructor overload one* @param i */ public Person(int i) { id=i; age=20; } /** * Constructor overload two* @param i * @param j */ public Person(int i,int j) { id=i; age=j; }}The above is a detailed explanation of the overloading of Java methods. I hope it will be helpful to everyone's learning.