Principle of remove overloaded method invocation for List interface of JAVA

Preface

To be honest, I usually read the source code by myself, and I rarely have the impulse to write it.
But when we write algorithms, we often use various sets in java, among which remove method is more commonly used.
remove has overloaded functions, and the parameters passed in are index or data object (automatic conversion after specifying generics). It's good to specify generics as other data types, but it's a bit confusing to specify Integer or int.
This once confused me, so I only use practice to solve the confusion.

Test class design

  • Test Class I
public class Text {

    public void remove(int index){
        System.out.println("Call the reference to int Of remove Method");
    }

    public void remove(Integer object){
        System.out.println("Call the reference to Integer Of remove Method");
    }

    public void remove(Object object){
        System.out.println("Call the reference to Object Of remove Method");
    }
}
  • Test Class II
public class Text {
    
    public void remove(Integer object){
        System.out.println("Call the reference to Integer Of remove Method");
    }

    public void remove(Object object){
        System.out.println("Call the reference to Object Of remove Method");
    }
}
  • Test Category III
public class Text {

    public void remove(Object object){
        System.out.println("Call the reference to Object Of remove Method");
    }
}

Result

Three test classes were introduced into int, Integer and Object variables to observe the effect.

  • Test Class I

    Input type int: Call remote method with parameter int
    Input type is Integer: Call remote method with reference to Integer
    The incoming type is Object: Call the remote method with the parameter Object

  • Test Class II

    Input type int: Call remote method with reference to Integer
    Input type is Integer: Call remote method with reference to Integer
    The incoming type is Object: Call the remote method with the parameter Object

  • Test Category III

    Input type int: Call remote method with parameter Object
    The incoming type is Integer: Call the remote method with the parameter Object
    The incoming type is Object: Call the remote method with the parameter Object

From the output results, it can be seen that when the class level of the method is gradually higher, the lower level of the method will be upgraded to meet the needs of the data transmission.

Cause analysis

Now let's decompile the source code of each test class. The results are as follows.

  • Test Class I

    invokevirtual #11 // Method remove:(I)V

    invokevirtual #15 // Method remove:(Ljava/lang/Integer;)V

    invokevirtual #18 // Method remove:(Ljava/lang/Object;)V

  • Test Class II

    invokevirtual #11 // Method remove:(Ljava/lang/Integer;)V

    invokevirtual #11 // Method remove:(Ljava/lang/Integer;)V

    invokevirtual #17 // Method remove:(Ljava/lang/Object;)V

  • Test Category III

    invokevirtual #10 // Method remove:(Ljava/lang/Object;)V

    invokevirtual #10 // Method remove:(Ljava/lang/Object;)V

    invokevirtual #10 // Method remove:(Ljava/lang/Object;)V

As you can see, decompiling code is all commands that call instance methods, so the automatic "upward transition" in the result is actually the contribution of jvm. JVM assigns static parameters to specific methods by determining the type of parameters invoked at compile time.
So the confusion in the preface has been relieved, because of the implementation of static dispatch in jvm, the order of invocation is int - > Integer - > Object.

Postscript

There's nothing to say. I feel that when I read the source code, I have to think about why I did it and why I did it. At the same time, I find my own answers by breakpoints or decompilation. keep going!

This article was first published in cartoon's blog
For reprinting, please indicate the source: Principle of remove overloaded method invocation for list interface of https://cartoonyu.github.io/cartoon-blog/post/java/java/

Tags: Java jvm github

Posted on Wed, 09 Oct 2019 09:14:45 -0700 by CONTEMAN