Sonar lint:不应检查 "compareTo" 结果的特定值

Sonar lint : "compareTo" results should not be checked for specific values

我正在比较两个值并得到声纳 lint 抛出 "Only the sign of the result should be examined" this issue

代码:

if (recBalanceAmt.compareTo(recRolloverEligibility) == 1) {
     recExpAmt = recBalanceAmt.subtract(recRolloverEligibility);
}

如何解决这个问题?

Sonar 建议检查 compareTo0 的结果,而不是 returns 直接 1-1

if (recBalanceAmt.compareTo(recRolloverEligibility) > 0) {

您可以在 compareTo() Javadoc

中找到此建议的原因

Returns: a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.