我有一个场景,从 UI 接收的对象传递一个 boolean 值,并将其作为整数存储在数据库中,如 TRUE=1 和 FALSE=0。现在,当标志从 ex 更改为 FALSE 时,我需要比较其整数值(如果为 0),则不执行任何操作,如果为 1,则更改为 0 并更新。一种方法如下,但仍然可能有更好的方法来做到这一点。
class AClient {
static Boolean x;
}
class BServer {
static Integer y;
}
public class Demo {
public static void main(String[] args) {
AClient.x = Boolean.TRUE;
BServer.y = 0;
System.out.println(storedValues());
}
private static Boolean storedValues() {
if (AClient.x) {
if (BServer.y.equals(new Integer(1))) {
return true;
} else {
return false;
}
} else {
if (BServer.y.equals(new Integer(1))) {
return false;
} else {
return true;
}
}
}
}
输出:假
最佳答案
您的storedValues
方法可以简化为:
return AClient.x.equals(BServer.y.equals(1));
如果x
和y
不需要为空,我会用基元而不是包装类替换它们,然后storedValues
可能看起来像这样:
return BServer.y == 1 == AClient.x;
您还可以将 storedValues
的返回类型更改为 boolean
。
关于java - 比较返回类型 Boolean 和 Integer 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27086282/