Similarities:
All methods in the same class have the same name, and different parameter Methods Form overload.
There will be a problem of type matching when passing parameters:
add(1,2) ;public void add(double a, float b) {...}public void add(float a, double b) {...}
Differences:
For C ++, overloading can also occur between the const and non-const versions with the same parameters. However, Java does not. For Java, functions of final and non-final versions with the same parameters do not constitute overloading. (The Compiler reports an error)
public void xx1(int a) { ... } public void xx1(final int c) { ... } /* these are not overload methods, the xx1 is repeaed defined */
For variable parameters
public void test (String msg) {...}public void test (String... msgs) {...}obj.test("Tom") ; /* there call the firest test *//* follow call the second test */obj.test("Evan, Tank) ;obj.test() ;
It can be seen that this method is called only when the method of the non-Variable Parameter exactly matches, otherwise it will call the variable parameter.
Ambiguity arising from overload of Variable Parameter Methods
public void go(int ... nums) {...} /* at least has 0 parameter */public void go(int a, int... nums) {...} /* at least has 1 parameter */public void go(int a, int b, int... nums) {...} /* at least has 2 parameters */ obj.go(1) ; /* can be called by firest and second method */ obj.go(1,2); /* can be called by 1st,2nd,3th methods */obj.go(1,2,3) ; /* can be called by 1st, 2nd, 3th methods */
In the preceding three go statements, the difference lies in the limit on the minimum number of parameters. If one calls the number of parameters it passes> = two or more overload functions, for this call, the overload method that satisfies his parameters is his best match, which may lead to ambiguity. For the above obj. Go (1) method, both the first and second methods are the best match, which leads to the ambiguity during compilation.