-1

I work on Java 8 and I have a simple issue that I have not figured out.

I have a 3 methods which are validating the data from db and returning true or false based on whether they got a row or not. The tricky part is, even if I know that the first part is returning false, I still want it to check for the other two methods. I have written my code like this :

boolean flag = true;

flag = flag && validateMethod1();
flag = flag && validateMethod2();
flag = flag && validateMethod3();

return flag;

The issue is, when validateMethod1() returns false, it is not calling validateMethod2(). Can someone explan why ? I tried this :

 boolean flag = true;

    flag = flag & validateMethod1();
    flag = flag & validateMethod2();
    flag = flag & validateMethod3();

    return flag;

Still face the same issue.

julien carax
  • 323
  • 6
  • 22

3 Answers3

4

&& is a short circuited AND operator. It doesn't evaluate the second operand if the first operand is evaluated to false.

In order to evaluate all the operands, use the non-short-circuit version of the AND operator :

return validateMethod1() & validateMethod2() & validateMethod3();

As you can see, your logic can be reduced to a single line of code.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • on one hand yes, on the other something went wrong when designing the code. – xenteros Dec 01 '16 at 09:21
  • what if I wanna have the old format ? Should I save it in seperate boolean variables and then use '&'? – julien carax Dec 01 '16 at 09:28
  • @AnkushBhan You can use your original code and just replace `&&` with `&`, but it doesn't look very elegant. Using separate boolean variables will also work (but then you can either use `&` or `&&`, since the AND operator will only be applied after the 3 methods are executed). – Eran Dec 01 '16 at 09:31
  • @Eran I understand, but what if I have more than 25 such methods and for each method I wanna check the boolean result and move to the next one ?BTW i used the '&' instead of '&&'. Still gives the same problem – julien carax Dec 01 '16 at 09:36
  • @AnkushBhan `&` should work fine. Try to add print statements to the validation methods and you'll see all of them are executed (unless some exception is thrown). – Eran Dec 01 '16 at 09:40
2

Java uses sort-circuit evaluation of expressions - i.e. as soon as it knows for sure the result will be false, it stops the evaluation.

Since your expression is a conjunction, once flag is found to be false, it is clear the whole expression will be false.

One way around it (quite verbose) is:

boolean flag1 = validateMethod1();
boolean flag2 = validateMethod2();
boolean flag3 = validateMethod3();
return flag1 && flag2 && flag3;
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
0

That's because you are using short circuit && instead of &, which leads to check only first argument in this case, when this argument is false.

The & operator, when used as logical operator, always evaluate both sides.

hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36