JAVA 对象/字符串方法重载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17669804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:36:43  来源:igfitidea点击:

JAVA Object/String method overload

java

提问by Aditya

Just out of curiosity I tried this example.

出于好奇,我尝试了这个例子。

public class Class1 {

    public void method(Object obj){
        System.out.println("Object");
    }

    public void method(String str){
        System.out.println("String");
    }

    public static void main(String... arg){
        new Class1().method(null);
    }

}

The output being "String". I want to know on what basis the JVM decides to invoke method taking String as argument and not Object.

输出为“字符串”。我想知道 JVM 在什么基础上决定调用以 String 作为参数而不是 Object 的方法。

采纳答案by Joni

Whenever more than one overloaded methods can be applied to the argument list, the most specific method is used.

每当可以将多个重载方法应用于参数列表时,将使用最具体的方法。

In this case either of the methods can be called when passing null, since the "null type" is assignable to both Objectand to String. The method that takes Stringis more specific so it will be picked.

在这种情况下,可以在传递时调用任何一个方法null,因为“空类型”可分配给ObjectString。采用的方法String更具体,因此将被选中。

回答by Reddy

Whenever there's Method Overloading, the JVM will search for the method from the most specific type to least specific type

每当出现方法重载时,JVM 都会从最具体的类型到最不具体的类型搜索方法

回答by veritas

See the JLS specification

请参阅JLS 规范

15.12.2.5. Choosing the Most Specific Method

15.12.2.5. 选择最具体的方法

It is one of the puzzle of Java Puzzlers by Joshua Bloch- Puzzle 46:Case of the Confusing Constructor

它是Joshua BlochJava Puzzlers谜题之一- Puzzle 46: Case of the Confusing Constructor

回答by Code Enthusiastic

Java Compiler chooses the most specific method.

Java Compiler 选择最具体的方法。

Stringis a more specific type compared to the Object.

Object相比,String是一种更具体的类型。

回答by Sandiip Patil

When you are doing method overloading, jvm tries to the next in hierarchy. For e.g. if you overload methods with long and int, but invoke method by passing byte, it will first go to int as it is next in hierarchy to byte.

当您进行方法重载时,jvm 会尝试层次结构中的下一个。例如,如果您使用 long 和 int 重载方法,但通过传递字节调用方法,它将首先转到 int,因为它在层次结构中是字节的下一个。

回答by Vimal Panchal

It's because of method Overloading

这是因为方法重载

The most specific method is chosen at compile time.

在编译时选择最具体的方法。

As 'java.lang.String' is a more specific type than 'java.lang.Object'. In your case it returns String.

因为“java.lang.String”是比“java.lang.Object”更具体的类型。在您的情况下,它返回字符串。

回答by Arvind Vishwakarma

Becaue you are passing nullin calling method and you defined void method(String str) And String always initlize with null. it will find that matching parametrized method. Thats y u got "str" on console.

因为你在调用方法中传递null并且你定义了 void method(String str) 并且 String 总是用 null initlize 。它会找到匹配的参数化方法。那就是你在控制台上得到了“str”。