Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of remove overloaded method invocation of JAVA's List interface

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the principle of remove overloaded method call of JAVA's List interface". The content of the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "what is the principle of remove overloaded method call of JAVA's List interface".

Preface

To tell you the truth, usually you understand the source code after reading it, and you seldom have the impulse to write it. However, when writing algorithms, we often use various sets in java, among which the remove method is also commonly used. Remove has overloaded functions, and the parameters passed in are index index or data Object (automatically converted after specifying generics). If you specify generics as other data types, but specify Integer or int, you may be a little confused. This has puzzled me once, so I have no choice but to use practice to solve the problem.

Test class design

Test class one

Public class Text {public void remove (int index) {System.out.println ("calling remove method passed as int");} public void remove (Integer object) {System.out.println ("calling remove method passed as Integer");} public void remove (Object object) {System.out.println ("calling remove method passed as parameter to Object");}}

Test class two

Public class Text {public void remove (Integer object) {System.out.println ("calling the remove method passed as Integer");} public void remove (Object object) {System.out.println ("calling the remove method passed as Object");}}

Test class three

Public class Text {public void remove (Object object) {System.out.println ("call the remove method passed as Object");}} result

The three test classes are passed into int,Integer and object variables respectively to observe the effect.

Test class one

The input type is int: call the remove method whose parameter is int and pass the type as Integer; call the remove method that passes the parameter as Integer and pass the type as Object: call the remove method that passes the parameter to Object

Test class two

The input type is int: call the remove method whose parameter is Integer and pass the type as Integer; call the remove method that passes the parameter as Integer and pass the type as Object: call the remove method that passes the parameter to Object

Test class three

The input type is int: call the remove method whose parameter is Object and pass the type as Integer; call the remove method that passes the parameter as Object and pass the type as Object: call the remove method that passes the parameter to Object

As can be seen from the output results, when the class level of passing parameters of the method gradually becomes higher, the lower-level passing parameters will be transformed upward to meet the needs of passing parameters.

Cause analysis

Let's decompile the source code of each test class first, and the result is as follows

Test class one

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

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

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

Test class two

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 class three

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, the decompiled code is full of commands that call instance methods, so the automatic "upward transformation" in the result is actually the credit of jvm. Jvm is statically dispatched to a specific method by determining the type of parameter to be called at compile time. So the confusion in the preface has been relieved because of the implementation of static dispatch in jvm, the calling order is int- > Integer- > Object.

Thank you for reading, the above is "what is the principle of remove overloaded method call of JAVA's List interface". After the study of this article, I believe you have a deeper understanding of what the principle of remove overloaded method call of JAVA's List interface is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report