Why FindBugs Shows Excess Zero Validation Warning

if (first != null && second != null && !first.equals(second)) { // not null & not equal } else if (first == null ^ second == null) { // not both null and not both not null // (first == null && second != null) || (first != null && second == null) } else { // both null or equal } 

FindBugs complains about else if (first == null ^ second == null) {...}

+7
java findbugs
source share
5 answers

Perhaps because it is only software.

0
source share

As you wrote in the comment: not both null good that FindBugs showed you your (potential) error, since you should use && (AND) not ^ (XOR):

 first != null && second != null 

or alternatively:

 !(first == null || second == null) 

UPDATE:
OP changes the comment to: "not both zero, not both non-zero" for this condition requires another if :

 (first == null && second != null) || (first != null && second == null) 

which is equivalent to:

 first == null ^ second == null 

just that the previous version is more readable.

0
source share

The ^ operator is a bitwise operator, not a logical operator. With a technical fix, operator precedence makes an expression confused if logical expressions grow. I do not use FindBugs, but I would call the suspect the third line - wrap it in parentheses or rewrite it.

  ... } else if ((first == null) ^ (second == null)) { ... 

^ can behave like a logical operation if the operands are logical values. Since the priority is different for each logical and bitwise operator, you should always group with parentheses, since the evaluation order will not be left to the right, but will be based on the table here: http://docs.oracle.com/javase/tutorial/java/ nutsandbolts / operators.html

Your expression "not as null" and "not not not null" is as follows:

(first == null) || second == null) && !((first == null && second == null))

This is pretty confusing, but this is what you are asking for.

I'm not sure what you are doing in blocks, but it might be easier to write the whole block as follows:

 if(first!=null && !first.equals(second)) { // first is not null and the first and second are not equal } else if (second!=null && !second.equals(first)) { // second is not null and first and second are not equal } else { // all that is left is that first and second are both null OR neither one is null but they are equal } 
0
source share

The warning reads: redundant zero checking . Thus, FindBugs believes that you are over-checking the invalidity of variables. Try if this code also triggers a warning:

  Boolean firstNull = (first == null); Boolean secondNull = (second == null); Boolean equalFirstSecond = first.equals(second); if (!firstNull && !secondNull && !equalFirstSecond) { // not null & not equal } else if (firstNull ^ secondNull){ // not both null and not both not null } else { // both null or equal } 
0
source share

if (first! = null && second! = null &, first.equals (second)) {

You do not need to test second != null here. A call to equals() does this.

} else if (first == null ^ second == null) {

In this case, you should return false , assuming that this is the equals() method.

If FindBugs does not like with this change, I would ignore it, he does not know what he is talking about. This is not perfect. Put an exception rule.

-2
source share

All Articles