JAVA method overloading

  • Method returns cannot be used as a basis for overloading
  • Static typing determines which method overload to use
  • Method overloading is selected by auto-boxing and enlarging the value of the underlying type

Let’s take a look at each of them

Method returns cannot be used as a basis for overloading

Methods with the same name but different formal parameters are called overloaded methods. The different formal parameters can be divided into different types of parameters and different sequences of parameters. The method name and its formal parameters are called the signature of the method. Method overloading is distinguished by the signature of the method, not its return value. Let me explain why the return value of a method is not a condition for method overload differentiation. There are two methods:

void f(a){}
int f(a){}
Copy the code

As long as the compiler can clearly determine the semantics based on the context, as in int x = f(), it can indeed distinguish overloaded methods. However, sometimes you don’t care about the return value, as in the following example:

f() ;
Copy the code

At this point, Java does not know which method to call. So it doesn’t work to distinguish method overloading by its return value.

Static typing determines which method overload to use

Here is a process that is often used as a Java interview question to explain method overloading.

public class StaticDispatch {
    static abstract class Human{}
    static class Man extends Human{}
    static class Woman extends Human{}
    public void sayHello(Human guy){ / / 1
        System.out.println("hello guy");
    }
    public void sayHello(Man guy){ / / 2
        System.out.println("Hello ,Man");
    }
    public void sayHello(Woman guy){ / / 3
        System.out.println("Hello ,Woman");
    }
    public static void main(String[] args) {
        Human man = new Man() ;
        Human woman = new Woman() ;
        StaticDispatch sr = newStaticDispatch() ; sr.sayHello(man); sr.sayHello(woman); }}//hello guy
//hello guy
Copy the code

We see that we pass in man and woman, and the output is hello Guy. The sayHello(Human guy) method in the code is called. So why is this method called? Let’s explain these two concepts first.

 Human man = new Man() ;
Copy the code

The Human of this code is called a static type, and the Man behind it is called the actual type. When a method is called, it looks at the static type of the parameter passed to the method, that is, Human, as a basis to find the matching method, because the static type of both objects is Human, so it calls the sayHello(Human guy) method.

Method overloading is selected by auto-boxing and enlarging the value of the underlying type

Above we looked at selecting method overloading by the static types of the parameters passed to the method. Here we’ll look at selecting method overloading by automatic boxing and other mechanisms. Let’s do that with a code.

public class Overload {
   public static void sayHello(char arg){ / / 1
        System.out.println("hello char");
    }
    public static void sayHello(int arg){/ / 2
        System.out.println("hello int");
    }
    public static void sayHello(long arg){/ / 3
        System.out.println("hello long");
    }
    public static void sayHello(float arg){/ / 4
        System.out.println("hello float");
    }
    public static void sayHello(double arg){/ / 5
        System.out.println("hello double");
    }
    public static void sayHello(Character arg){/ / 6
        System.out.println("hello Character");
    }
    public static void sayHello(Serializable arg){/ / 7
        System.out.println("hello Serializable");
    }
    public static void sayHello(Object arg){/ / 8
        System.out.println("hello object");
    }
    public static void sayHello(char. arg){/ / 9
        System.out.println("hello char...");
    }

    public static void main(String[] args) {
        sayHello('a'); }}//hello char
Copy the code

The above code will no doubt print Hello char. SayHello (int arg) is called if the sayHello(char arg) method is commented out. The sayHello(int arg) method is called when ‘a’ represents 97 (the Unicode value of the character ‘a’ is the decimal number 97), because the sayHello(int arg) method cannot be found. Similarly, when a method of type int is not found, a method of type long is called. The search order is char, int, long, float, double. When all five methods are commented out, the sayHello method of type Character is called. An automatic boxing occurs, and ‘a’ is wrapped with its encapsulation type java.lang.Character.

When we comment out methods of type Character as arguments, we see that the output is Hello Serializable. That’s when it gets confusing. If you can’t find a method whose type is Character, you can find the parent of the method. Serializable is the parent of Character. If you can comment out the method whose type is Serializable, you can print hello object. This is because Object is the parent of all classes that are not shown to inherit from other classes. When we comment out sayHello(Object arg), we call sayHello(char… Arg) method. Therefore, it can be seen that variable-length parameters have the lowest overload priority.

Summarize the steps above for finding method overloads

  • Char, int, long, float, double
  • When the above can not be found, automatic boxing will occur, to select the auto-boxing class, and then according to the parent class layer by layer to find
  • Finally, you can see the overloading of variable length parameters

Ok, so those are some of the special things about method overloading. By the way, if you can answer any of these questions in an interview, your interviewer will be impressed. Most people only know the definition of method overloading, not some of its features. Finally, I want to talk about the importance of basic knowledge, especially when fresh graduates go for interviews. There are a lot of people who take the wrong path and focus on learning various frameworks. In fact, in my interview experience, most interviewers like to study the basics, especially if you have a deeper understanding of some of the basics than others, then you have a better chance of getting an offer.

Finally, please pay attention to my wechat official account “Javams”. I have prepared a very detailed interview question for you, and you can get it by answering the key word “interview”. Meanwhile, I hope you can discuss your learning experience and techniques with me.