Home >Java >javaTutorial >Overloading in Java can achieve different return values
Overloading in Java cannot achieve different return values. Method overloading has nothing to do with the return value type and modifiers. Overloading in Java occurs in this class, and the condition for overloading is in this class. There are multiple methods with the same name but different parameter lists, which have nothing to do with the return value.
Overloading in Java cannot return different values
Method overloading is related to the return value type and Modifiers are irrelevant, Java overloading occurs in this class. The condition for overloading is that there are multiple methods with the same name in this class, but different parameter lists (maybe the number of parameters is different, the parameter type different), has nothing to do with the return value.
We can give an example, if we write two methods in the same class with the same name and the same parameter list, but the return value is different,
public Stringshow(int number) public int show(int number)
And we call this method When assigning a value to it, the Java virtual machine does not know whether you want to assign it to a String type method or an int type method, so overloading has nothing to do with the return value.
Related learning recommendations: java basics
The above is the detailed content of Overloading in Java can achieve different return values. For more information, please follow other related articles on the PHP Chinese website!