Apache下的common -lang工具包下的BooleanUtils类的xor(true, true, true)



xor(true, true, true)的结果 布尔utilscommon_lang 下的类false,但是System.out的结果。println(true ^ true ^ true)真正是. 为什么?

public class Test {
public static void main(String[] args) {
System.out.println(org.apache.commons.lang.BooleanUtils.xor(new boolean[]{true, true, true}));
System.out.println(org.apache.commons.lang3.BooleanUtils.xor(new boolean[]{true, true, true}));
System.out.println(true ^ true ^ true);
}
}
/*
result:
false
false
true
*/

您看到这种行为的最有可能的原因是您正在使用旧版本的commons-lang (<</p> 3.2)。

新版本的行为与Java相同(即从左到右一次计算一个xor)。

然而,旧版本使用了不同的方法:只有当恰好有一个时,它们才返回true。

此行为被认为是不正确的(参见LANG-921),并且已经修复。

相关内容

最新更新