java例外。lang.IllegalArgumentException:比较法违反其一般合同含义?
现在我知道之前已经有人问过关于这个主题的问题,但是我的问题并不是关于一个特定的例子
我只是想了解所述错误的含义。我在互联网上所能找到的只是基于示例的解决方案。有人能解释一下这个错误的核心是什么,什么时候会发生
我只是一个试图学习的学生,无法通过其他渠道理解这一点,所以请耐心等待我
谢谢
你可以在下面搜索框中键入要查询的问题!
现在我知道之前已经有人问过关于这个主题的问题,但是我的问题并不是关于一个特定的例子
我只是想了解所述错误的含义。我在互联网上所能找到的只是基于示例的解决方案。有人能解释一下这个错误的核心是什么,什么时候会发生
我只是一个试图学习的学生,无法通过其他渠道理解这一点,所以请耐心等待我
谢谢
# 1 楼答案
如果您不想要一个具体的例子,只需转到合同的规范,例如可以在比较器javadoc中找到:
等等。更重要的是compare()方法的javadoc:
这些规范定义了“合同”的内容。当你违反了合同,并且系统能够检测到,你最终会出现上述异常
# 2 楼答案
Comparable定义了
compareTo
方法的约定:如果实现者没有确保这些条件,则违反了合同,
Comparable.compareTo
方法的用户可以报告,但有例外