1

Ok, i am building program to check many fields. If at least 1 field is not ok then i don't want my program to spend time to check other fields. So let look at this code:

 // Util.isReadyToUse method return true if the string is ready for using, & return false if it is not.

boolean isOK=true;

if(!Util.isReadyToUse(firstName)){
   isOK=false;
}
else if(isOK && !Util.isReadyToUse(lastName)){
   isOK=false;
}
else if(isOK && !Util.isReadyToUse(email)){
   isOK=false;
}
.....more checking
if(isOK) {
   //do sthing
}

Ok, when running, the program will first check !Util.isReadyToUse(firstName). Suppose it returns (isOK=false). Next the program will check isOK && !Util.isReadyToUse(lastName).

So my question here is that Since the isOK currently false, then will the program spend time to check the condition !Util.isReadyToUse(lastName) after &&?

Ok, As a human being, if you see isOK=false and now you see isOK && !Util.isReadyToUse(email), then you don't want to waste time to look at !Util.isReadyToUse(email) since isOK=false and u saw && after isOK.

Will machine also work like that?

I am thinking to use break but why people say break doesn't work in if statement:

if(!Util.isReadyToUse(firstName)){
   isOK=false;
   break;
}
else if(isOK && !Util.isReadyToUse(lastName)){
   isOK=false;
   break;
}......

What is the best solution in this situation?

Tum
  • 3,614
  • 5
  • 38
  • 63
  • Your program does not do your first statement `Suppose it returns (isOK=false). Next the program will check isOK && !Util.isReadyToUse(lastName)` as you have an else – RamonBoza Oct 31 '13 at 15:08
  • 1
    No. In Java `&&` is a short-circuit operator. See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html – Reinstate Monica -- notmaynard Oct 31 '13 at 15:09
  • at the beginning isOK=true, so it will check – Tum Oct 31 '13 at 15:09
  • what u mean short-circuit operator? – Tum Oct 31 '13 at 15:10
  • why don't you try putting a printout in the second conditions and see what happens. – Paul Rubel Oct 31 '13 at 15:10
  • 1
    Possible duplicate here? http://stackoverflow.com/questions/8572565/how-does-java-deal-with-multiple-conditions-inside-a-single-if-statement – Alex Oct 31 '13 at 15:12
  • 1
    It will not execute the second condition, and you can rely on that not just for performance, but for functional correctness. Something like `if(isOkay && thisMethodIsOnlyValidIfIsOkayIsTrue())` is perfectly fine and fairly standard, especially in the context of null checks: `if (a != null && a.doSomething()) ...` – yshavit Oct 31 '13 at 15:12

6 Answers6

7

So my question here is that Since the isOK currently false, then will the program spend time to check the condition !Util.isReadyToUse(lastName) after &&?

Java is smart, if you have a condition if(somethingFlase && something), then something won't be reached due to Short-circuit evaluation. Since the whole expression will be false regardless of the second condition, there is no need for Java to evaluate that.

From 15.23. Conditional-And Operator &&:

If the resulting value is false, the value of the conditional-and expression is false and the right-hand operand expression is not evaluated. If the value of the left-hand operand is true, then the right-hand expression is evaluated.

  • if(a && b) - if a is false, b won't be checked.
  • if(a && b) - if a is true, b will be checked, because if it's false, the expression will be false.
  • if(a || b) - if a is true, b won't be checked, because this is true anyway.
  • if(a || b) - if a is false, b will be checked, because if b is true then it'll be true.
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 3
    @MinhHai Yes, sometimes it can even walk your dog :) – Maroun Oct 31 '13 at 15:12
  • so if i swap like this "if(!Util.isReadyToUse(lastName) && isOK)" then the program will check "!Util.isReadyToUse(lastName)" even isOK=false, right? – Tum Oct 31 '13 at 15:17
  • @MinhHai Yes, the order is important, it's from left to right. – Maroun Oct 31 '13 at 15:18
2

No, it shortcuts the rest of the predicate.

That's you'll see things like

if(A != null && A.SomeVal == someOtherVal)
1

Java supports what is referred to as Short-Circuit Evaluation. See this page:

http://en.wikipedia.org/wiki/Short-circuit_evaluation

What this means is that if the first boolean in your statement is enough to satisfy the statement, then the rest of the values are skipped. If we have the following:

boolean a = false;
boolean b = true;

if(a && b) /*Do something*/;

'b' will never be checked, because the false value for 'a' was enough to break out of the if statement.

That being said, your program will never take advantage of this because the only time isOK is set to false is within one of your else if statements.

CodeBlind
  • 4,519
  • 1
  • 24
  • 36
1

As the other responders mentioned Java will do the smart thing.

But it could be the case that you want Java to continue to check, in that case you can use & vs && or | vs ||.

if (someMethod() | anotherMethod() {

If the first method reutrns true, Java will still execute the second method.

if (someMethod() & anotherMethod() {

If the first method is false, Java will still execute the second method.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
  • Please don't use that second one. The only reason it would make sense is if `anotherMethod()` causes needed side effects, and invoking side effects in an if statement is a really bad idea. – Ian McLaird Oct 31 '13 at 15:24
1

No, Java won't "waste time" for it. It's called short circuit evaluation.

This mechanism is commonly used e.g. for null checking :

if (foo != null && foo.neverFailsWithNPE()) {
    // ...
}
Danstahr
  • 4,190
  • 22
  • 38
0

You don't need to use break on an if..else if.. else statement because once it finds a condition which is true the rest aren't even looked at.