Java allows you to define multiple methods with the same name in a class, but the type or number of parameters must be different, which is the overload of the method.
Public classMethodDemo03 { Public Static voidMain (string[] args) {//The following is a call to the summation method intSUM1 = Add (1, 2); intsum2 = Add (1, 2, 3); DoubleSUM3 = Add (1.2, 2.3); //The following code is the result of printing the sumSystem.out.println ("sum1=" +sum1); System.out.println ("Sum2=" +sum2); System.out.println ("Sum3=" +sum3); } //The following method implements the addition of two integers Public Static intAddintXinty) {returnX +y; } //The following method implements the addition of three integers Public Static intAddintXintYintz) {returnX + y +Z; } //The following method implements the addition of two decimals Public Static DoubleAddDoubleXDoubley) {returnX +y; }}
When the Add () method is called in the main () method, it is possible to determine which overloaded method is called by passing in different parameters, such as Add, which is a method of summing two integers. It is worth noting that the overloads of a method are independent of the return value type, which has only two conditions, one is the same method name, the other is the number of arguments or the parameter type is different.
L Overloaded method parameters must be different:
Different number of arguments, such as method (int x) and method (int x,int y)
parameter types are different, such as method (int x) and method (Double x) different from G
parameter order differs, such as method (int x,double y) differs from method (double x,int y)
The L overload is only related to the method name and the parameter type, regardless of the return value
such as void method (int x) and int method (int y) are not methods overloads and cannot exist concurrently
L overloads are independent of specific variable identifiers
such as method (int x) and approach (int y) are not methods overloaded and cannot exist simultaneously
Java Method overloading