If you are reading this, chances are you got The method X is ambiguous for the type Y error when compiling a java program in terminal or in any Java IDE.

如果您正在阅读本文,则很可能在终端或任何Java IDE中编译Java程序时The method X is ambiguous for the type Y错误The method X is ambiguous for the type Y

Java模棱两可的方法调用 (Java ambiguous method call)

Here I am going to explain why java ambiguous method call error comes with some examples. This ambiguous method call error always comes with method overloading where compiler fails to find out which of the overloaded method should be used.

在这里,我将解释为什么Java歧义方法调用错误带有一些示例。 这种模棱两可的方法调用错误总是伴随着方法重载,而编译器无法找出应该使用哪个重载方法。

Suppose we have a java program like below.

假设我们有一个如下的Java程序。

package com.journaldev.errors;public class Test {public void foo(Object o) {System.out.println("Object");}public void foo(String s) {System.out.println("String");}public static void main(String[] args) {new Test().foo(null);}}

Above program compiles perfectly and when we run it, it prints “String”.

上面的程序可以完美地编译,当我们运行它时,它会显示“ String”。

So the method foo(String s) was called by the program. The reason behind this is java compiler tries to find out the method with most specific input parameters to invoke a method. We know that Object is the parent class of String, so the choice was easy. Here is the excerpt from Java Language Specification.

因此,程序调用了foo(String s)方法。 这背后的原因是Java编译器试图找出具有最特定输入参数的方法来调用方法。 我们知道Object是String的父类,因此选择很容易。 这是Java语言规范的摘录。

If more than one member method is both accessible and applicable to a method invocation … The Java programming language uses the rule that the most specific method is chosen.
如果可以访问多个成员方法并将其应用于方法调用……Java编程语言使用选择最具体方法的规则。

The reason I am passing “null” is because it works for any type of arguments, if we pass any other objects the choice of method for the java compiler is easy.

我传递“ null”的原因是因为它适用于任何类型的参数,如果我们传递任何其他对象,则为Java编译器选择方法很容易。

方法X对于类型Y是不明确的 (The method X is ambiguous for the type Y)

Now let’s add below method to the above code.

现在,将以下方法添加到上面的代码中。

public void foo(Integer i){System.out.println("Integer");
}

You will get compile time error as The method foo(Object) is ambiguous for the type Test because both String and Integer class have Object as parent class and there is no inheritance. So java compiler doesn’t consider any of them to be more specific, hence the method ambiguous call error.

您将获得编译时错误,因为The method foo(Object) is ambiguous for the type Test因为String和Integer类都将Object作为父类,并且没有继承。 因此,java编译器不认为它们中的任何一个更加具体,因此该方法模棱两可的调用错误。

package com.journaldev.strings;public class Test {public void foo(Object o) {System.out.println("Object");}public void foo(Exception e) {System.out.println("Exception");}public void foo(NullPointerException ne) {System.out.println("NullPointerException");}public static void main(String[] args) {new Test().foo(null);}}

As above explained, here foo(NullPointerException ne) is the most specific method because it’s inherited from Exception class and hence this code compiles fine and when executed prints “NullPointerException”.

如上所述,此处的foo(NullPointerException ne)是最具体的方法,因为它是从Exception类继承的,因此此代码可以正常编译,并且在执行时显示“ NullPointerException”。

I hope this article clarifies any doubt you have with java ambiguous method call compiler error, please comment if you want to add something or if you have some confusion with this.

我希望本文能澄清您对java歧义方法调用编译器错误的任何疑问,如果要添加某些内容或对此有疑问,请发表评论。

翻译自: https://www.journaldev.com/9107/the-method-is-ambiguous-for-the-type-java-ambiguous-method-call-null-error

“方法X对于类型Y是模糊的” Java模糊方法调用null错误相关推荐

  1. list 获取元素 java_获取List中元素的类型,其中List是Java中方法的返回类型

    小编典典 您可以使用getGenericReturnTypewhich返回a Type而不是a Class,并允许您获取所有类型参数等.简短而完整的示例: import java.lang.refle ...

  2. idea查看方法的返回类型和自动出来变量

    idea查看方法的返回类型: //解析request,需要抛异常upload.parseRequest(request); 比如说这时候,要出来,parseRequest()方法的返回类型 这时候可以 ...

  3. 零基础全方位学习java的方法

    java技术岗是一个就业率非常高的岗位,因此有很多人都想要学习java技术,其中不乏一些零基础同学,零基础小白都不清楚自己该如何学习java,那么下面小编就为大家详细的介绍一下零基础全方位学习java ...

  4. java void方法_Java对象类的最终void wait(long ms)方法,包含示例

    java void方法 对象类最终无效等待(长毫秒) (Object Class final void wait(long ms)) This method is available in java. ...

  5. java本地方法出现问题怎么办_Java调用本地方法又是怎么一回事

    JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...

  6. javascript调用java的方法

    这篇文章主要讲述如何在JavaScript脚本语言中调用Java类和接口,对大家的学习和工作有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧. 前言 本文中所有的代码使用 JavaScript 编 ...

  7. java重载方法与构造方法 区别

    java构造方法与重载方法 1.什么是构造方法和重载方法呢? 通俗来讲构造方法就是与类名相同的方法,而重载方法则是一个类中有多个相同方法名的方法 构造方法特点: 1.不需要void关键字修饰2.不需要 ...

  8. Part III.S1. 基于离差最大化的直觉模糊多属性决策方法

    1.1 属性权重完全未知情形下的直觉模糊多属性决策方法 1.1.1 问题描述   设某多属性决策问题有 m m m个方案 Y i ( i = 1 , 2 , - m ) Y_{i}\left(i=1, ...

  9. Part III.S3. 对方案有偏好的直觉模糊多属性决策方法

    3.1 问题描述   设某多属性决策问题有 m m m个方案 Y i ( i = 1 , 2 , ⋯ , m ) Y_{i}\left(i=1,2,\cdots,m\right) Yi​(i=1,2, ...

最新文章

  1. sonar的安装以及使用
  2. iPhone开发过程中调试多次Release问题 message sent to deallocated
  3. UML用例图总结(转)
  4. 程序员是一盏省油的灯
  5. 【1291】数据结构上机测试4.1:二叉树的遍历与应用1 SDUTOJ
  6. SpringMVC控制器单例和多例
  7. 《软技能—代码之外的生存指南》
  8. 【数据结构】二叉搜索树的python实现
  9. 042 实例10-文本词频统计
  10. c语言中输出值作用,printf在c语言中什么意思
  11. 移动光猫固件备份、刷机、改sn和mac等
  12. java做 binggo,CONTRIBUTING.md
  13. 百度地图高德地图谷歌地图腾讯地图商家数据采集
  14. A19:Unity(C#)获取当前运行exe路径的方法
  15. 数据模型,数据模型概述,数据模型基本概念
  16. Pytorch中tensor.expand()和tensor.expand_as()函数
  17. python opc服务器和客户端互相发送信息并给出实例
  18. Fatal Python error: Aborted
  19. WebApi接口 - 如何在应用中调用webapi接口
  20. 使用图片精灵优化前端 减少http请求

热门文章

  1. [LeetCode] Merge Sorted Array
  2. [转载] python不允许使用关键字_Python中关键字global与nonlocal的区别
  3. HDU 6336 Matrix from Arrays (杭电多校4E)
  4. layui结合mybatis的pagehelper插件的分页通用的方法
  5. mysql 日期计算,今天,明天,本周,下周,本月,下月
  6. Android ClassLoader笔记(二)
  7. 第三百八十一节,Django+Xadmin打造上线标准的在线教育平台—xadmin全局配置
  8. Java foreach
  9. sql server中除数为零的处理技巧
  10. CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡