Method overloading refers to a method that has multiple methods of the same name in the same class, but each method has a unique parameter list.

// When an overloaded method is called, the system automatically calls the corresponding method according to its parameters
public class Demo{

    public void function(String a){
        System.out.println("function(String)");
    }
    public void function(String a,int b){
        System.out.println("function(String,int)");
    }
    // Depending on the order of the arguments, it is possible to overload methods, but it can be confusing
    public void function(int b,String a){
        System.out.println("function(int,String)"); }}Copy the code

Methods can only be overloaded based on arguments, not by the type of return value, for example:

// Wrong example
public class Demo{
    public int function(String a){
        System.out.println("function(String)");
        return 1;
    }
    public void function(String a){}}Copy the code

Int a = function(“123”); function(“123”); function(“123”);