2

Possible Duplicate:
Does Java evaluate remaining conditions after boolean result is known?

When executing code with multiple && statements in java, does the compiler continue to resolve additional boolean comparisons if the first one resolves to false?

if (1==2 && 3==4 && 4==5) { Do Something }

Once determining 1==2 is false, will the compiler immediately break out of the if statement or will it continue to resolve 3==4 and 4==5 after?

Community
  • 1
  • 1
Tobyz28
  • 23
  • 4

7 Answers7

2

In the case of && it'll stop evaluating the moment it detects that one of the conditions is false. This is called short circuit evaluation.

Sean
  • 60,939
  • 11
  • 97
  • 136
2

"SHORT CIRCUIT EVALUATION IN PROGRAMMING LANGUAGES"

In the case of any logical expression most compiler stop evaluation expression as soon as result evaluated. its not only in Java but in almost in every language. (but not all e.g. VB6)

You can also check it as follows:

i = 0 , j =1;
i && ++j;
system.out.print(" j=" + j);

The value of j will be 1, it means ++j was not executed.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

does the compiler continue to resolve additional boolean comparisons if the first one resolves to false

The short answer. No! The compiler javac and the JIT analyses statically all the code. It doesn't take short cuts like this. e.g. if the second condition doesn't compile, you will always get a compilation error regardless of the first condition.

What you may have intended to ask is; will the program execute the other conditions if the first or second one is false In this case, it will not.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

This doesn't make a difference for the compiler -- all it does is resolve the boolean comparisons into machine code, represented by one or more .class files.

As far as the actual runtime... If I remember correctly from Computer Science class, both Tarun's and Lews's answers are correct -- the comparison will short-circuit as soon as it gets to an expression that isn't true.

stiggs
  • 303
  • 4
  • 9
0

IF "1==2" is false than the compiler will immediately break out the if statement.

Tarun Gupta
  • 1,232
  • 2
  • 13
  • 26
0

&&

- The above is a short-circuit AND operator. If the 1st condition results in false the 2nd condition is not evaluated.

Eg:

if ((a==false) && (b==true)){


  }

The above will stop after the first evaluation.

- If you want to force the evaluation of both the conditions, then try Non-Short Circuit AND (&)

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

A sidenote to the keyword 'Efficiency' in your title :- as stated by other answers your basic question is easily answered, so it's not a question of efficiency, but a question if you need for some reason all expressions to be evaluated, for example when executing code which changes state (which would be by the way not good coding at all).

But if you think of 'Efficiency' in terms of executing speed, then you almost never have to optimize on that level, either the compiler will do it for you or you wouldn't notice the difference. It may be important in some time critical operation or in operations which run in very big loops, but in nearly every other case, it's much more important to write clean code, so that another human can read it well.

Tarun Gupta
  • 1,232
  • 2
  • 13
  • 26
Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96