[英]The result of xor(true, true, true) of the BooleanUtils class under the commons-lang toolkit under Apache is false, why?
The result of xor(true, true, true) of the BooleanUtils class under the commons-lang toolkit under Apache is false , but the result of System.out.println(true ^ true ^ true) is true . Apache下commons-lang工具包下BooleanUtils类的xor(true, true, true)结果为false ,而System.out.println(true ^ true ^ true)的结果为true 。 Why?
为什么?
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);
}
}
/*
打印结果:
false
false
true
*/
The most likely reason you are seeing this behavior is that you are using an older version of commons-lang (< 3.2).您看到此行为的最可能原因是您使用的是旧版本的 commons-lang (< 3.2)。
Newer versions behave the same as Java (ie it evaluates one xor at a time from left to right).较新版本的行为与 Java 相同(即从左到右一次计算一个异或)。
The older versions used a different approach however: They return true only if there is exactly one true value in the entire array.然而,旧版本使用了一种不同的方法:只有当整个数组中只有一个真值时,它们才返回真值。
This behavior was considered incorrect (see LANG-921 ) and has since been fixed.此行为被认为是不正确的(请参阅LANG-921 )并已修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.