• <legend id='679jX'><style id='679jX'><dir id='679jX'><q id='679jX'></q></dir></style></legend>

      <bdo id='679jX'></bdo><ul id='679jX'></ul>
    <i id='679jX'><tr id='679jX'><dt id='679jX'><q id='679jX'><span id='679jX'><b id='679jX'><form id='679jX'><ins id='679jX'></ins><ul id='679jX'></ul><sub id='679jX'></sub></form><legend id='679jX'></legend><bdo id='679jX'><pre id='679jX'><center id='679jX'></center></pre></bdo></b><th id='679jX'></th></span></q></dt></tr></i><div id='679jX'><tfoot id='679jX'></tfoot><dl id='679jX'><fieldset id='679jX'></fieldset></dl></div>
  • <small id='679jX'></small><noframes id='679jX'>

    1. <tfoot id='679jX'></tfoot>

      1. 在函数重载的情况下,JVM 如何找到要调用的方法(最匹配的参数)

        How JVM finds method (parameter with the closest matching) to call in case of function overloading(在函数重载的情况下,JVM 如何找到要调用的方法(最匹配的参数))

      2. <i id='Cl30i'><tr id='Cl30i'><dt id='Cl30i'><q id='Cl30i'><span id='Cl30i'><b id='Cl30i'><form id='Cl30i'><ins id='Cl30i'></ins><ul id='Cl30i'></ul><sub id='Cl30i'></sub></form><legend id='Cl30i'></legend><bdo id='Cl30i'><pre id='Cl30i'><center id='Cl30i'></center></pre></bdo></b><th id='Cl30i'></th></span></q></dt></tr></i><div id='Cl30i'><tfoot id='Cl30i'></tfoot><dl id='Cl30i'><fieldset id='Cl30i'></fieldset></dl></div>

      3. <tfoot id='Cl30i'></tfoot>
        <legend id='Cl30i'><style id='Cl30i'><dir id='Cl30i'><q id='Cl30i'></q></dir></style></legend>
        • <bdo id='Cl30i'></bdo><ul id='Cl30i'></ul>

              <small id='Cl30i'></small><noframes id='Cl30i'>

                    <tbody id='Cl30i'></tbody>

                • 本文介绍了在函数重载的情况下,JVM 如何找到要调用的方法(最匹配的参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  JVM 决定在编译时调用哪个重载方法.我有一个例子:

                  The JVM decides which overloaded method to call at compile time. I have one example:

                  public class MainClass{
                  
                    public static void go(Long n) {System.out.println("takes Long ");}
                    public static void go(Short n) {System.out.println("takes Short ");}
                    public static void go(int n) {System.out.println("takes int ");}
                  
                    public static void main(String [] args) {
                      short y = 6;
                      long z = 7;
                      go(y);
                      go(z);
                      go((Short)y);
                    }
                  }
                  

                  根据我的理解,它应该打印以下内容:

                  According to my understanding, it should print the following:

                  takes Short
                  takes Long
                  takes Short
                  

                  ...但实际输出是:

                  takes int
                  takes Long
                  takes Short
                  

                  但是如果我有以下三个功能:

                  However if I have the following three functions:

                  public static void go(Integer n) {System.out.println("takes Integer");}
                  public static void go(Long n) {System.out.println("takes Long ");}
                  public static void go(Short n) {System.out.println("takes Short ");}
                  

                  ...并使用以下方法调用它:

                  ... and call it using:

                  int a= 10; and go(i);  //output : takes Integer.
                  

                  ...为什么shortint有区别?

                  ... why there is there a difference for short and int?

                  推荐答案

                  参见JLS 第 15.12.2 节,规则编译器遵循以确定调用哪个方法.编译器总是选择最具体的方法,以防你的方法被重载:

                  See JLS Section 15.12.2, for rules compiler follows to determine which method to invoke. Compiler always chooses the most specific method in case your methods are overloaded:

                  这样的方法可能不止一种,在这种情况下,选择最具体的一种.最具体方法的描述符(签名加返回类型)是在运行时用于执行方法分派的描述符.

                  There may be more than one such method, in which case the most specific one is chosen. The descriptor (signature plus return type) of the most specific method is one used at run time to perform the method dispatch.

                  编译器首先尝试在没有装箱或拆箱的情况下解析方法:

                  Compiler first tries to resolve the method without boxing or unboxing as quoted there:

                  第一阶段(第 15.12.2.2 节)执行重载决议不允许装箱或拆箱转换,或使用可变的 arity 方法调用.如果在此阶段没有找到适用的方法,则处理继续到第二阶段.

                  The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

                  强调我的.

                  所以,在您的 1st 代码中,因为 short 可以用作 int 类型参数的参数.编译器不会使用带有参数 Short 的方法,因为这需要装箱.而在 long 类型的情况下,由于它不能用作 int 类型的参数,因此将其装箱为 Long.请记住加宽比拳击更受欢迎.

                  So, in your 1st code, since short can be used as argument for parameter of type int. Compiler will not use the method with parameter Short, as that requires boxing. While in case of long type, since it cannot be used as argument for type int, it goes for boxing it to Long. Remember Widening is preferred over Boxing.

                  在您的 2nd 中,除了将 int 装箱到 Integer 之外别无他法.因此,它调用带有 Integer 参数的方法.

                  In your 2nd, there is no other way than boxing int to Integer. So, it calls method with Integer parameter.

                  这篇关于在函数重载的情况下,JVM 如何找到要调用的方法(最匹配的参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                  <legend id='Ru5Ra'><style id='Ru5Ra'><dir id='Ru5Ra'><q id='Ru5Ra'></q></dir></style></legend>

                        <small id='Ru5Ra'></small><noframes id='Ru5Ra'>

                          <tbody id='Ru5Ra'></tbody>
                      • <i id='Ru5Ra'><tr id='Ru5Ra'><dt id='Ru5Ra'><q id='Ru5Ra'><span id='Ru5Ra'><b id='Ru5Ra'><form id='Ru5Ra'><ins id='Ru5Ra'></ins><ul id='Ru5Ra'></ul><sub id='Ru5Ra'></sub></form><legend id='Ru5Ra'></legend><bdo id='Ru5Ra'><pre id='Ru5Ra'><center id='Ru5Ra'></center></pre></bdo></b><th id='Ru5Ra'></th></span></q></dt></tr></i><div id='Ru5Ra'><tfoot id='Ru5Ra'></tfoot><dl id='Ru5Ra'><fieldset id='Ru5Ra'></fieldset></dl></div>
                          <bdo id='Ru5Ra'></bdo><ul id='Ru5Ra'></ul>

                          <tfoot id='Ru5Ra'></tfoot>