java - 为什么两个带有签名(primitive, wrapper) 和(primitive, primitive) 的方法会导致方法调用(wrapper, primitive) 不明确?

标签 java java-6

这只是一个练习,但我无法弄清楚其中的歧义:

private static void flipFlop(String str, int i, Integer iRef) {
System.out.println(str + "ciao");
}

private static void flipFlop(String str, int i, int j) {
System.out.println(str + "hello");
}

public static void main(String[] args) {
flipFlop("hello", new Integer(4), 2004);
}

它说:

The method flipFlop(String, int, Integer) is ambiguous for the type Test

我猜想第二个参数会被解包为 int,因此第二个 flipFlop 方法会成为选择。

最佳答案

如果您喜欢引人入胜的阅读,here is the relevant portion of the Java Language specification描述了方法是如何解析的。

但基本上你的第三个参数可以被解释为一个原始的或一个自动装箱的包装器,编译器无法弄清楚你想要什么。这两种方法都“最具体”以使用 JLS 术语。

关于java - 为什么两个带有签名(primitive, wrapper) 和(primitive, primitive) 的方法会导致方法调用(wrapper, primitive) 不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20923888/

相关文章:

java - compare() 中的空字段怎么办?

java - 如何判断 HttpClient.execute() 何时完成获取大型请求中的所有内容?

java - 是否有 Java 实用程序可以将字符串路径转换为使用正确的文件分隔符字符?

java - 将图像添加到另一图像的顶部

java - ArrayIndexOutOfBoundsException 错误

java - 删除 v7 appcompat 文件夹

java - 延迟来自客户端的并发请求,直到创建 HttpSession

java - Netbeans找不到JDK和JRE

java - 在 Java 中,是否可以使用资源路径获取 WAR 类路径中的目录文件列表?

java - 在 JPA 中映射复杂的 Map