如何在Java中将返回类型为整数的一个getter方法与一个常量整数变量进行比较

huangapple go评论59阅读模式
英文:

How to compare one getter method with return type as integer with a constant int variable in java

问题

这是我的代码:

public boolean isCalled() {
    if (getABC() == Constants.XYZ)
        return true;
    else
        return false;
}

我需要在比较函数中获得一个布尔返回值。有没有一种类似于 StringUtils.equals 用于比较字符串的智能方法呢?

英文:

Here is my code :

public boolean isCalled() {
 if(getABC() == Constants.XYZ)
    return true;
  else
    return false;
  }

I need to get a boolean return value of my compare function. Is there a way to do this in smart way (just like StringUtils.equals for comparing strings)?

答案1

得分: 2

你不需要那个 if== 返回一个布尔值,你可以直接返回它:

public boolean isCalled() {
    return getABC() == Constants.XYZ;
}
英文:

You don't need the if. The == returns a boolean, and you can just return this directly:

public boolean isCalled() {
    return getABC() == Constants.XYZ;
}

huangapple
  • 本文由 发表于 2020年9月10日 16:57:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63826225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定