Reputation: 1013
I'm just curious about how Java actually works when it come to if
statements. (Note: when I say "component" below I mean the idividual parts checked by the statement, e.g. a
, b
, c
)
Which is more efficient in terms of calculations?
if (a && b && c) { do stuff }
or
if (a) {
if (b) {
if (c) {
do stuff }
}
}
The reason why I ask is because it's important what Java does in the first version. Does it check every single thing in the statement or does it check a
and if it is false
then cancel checking the rest of the statement?
If this is the case then it makes sense to put the component most likely to fail as the first component in the statement.
If the whole statement is checked every time then it makes more sense to split the components into a bunch of different statements, as in the second example.
Upvotes: 7
Views: 4725
Reputation: 20061
I was curious how the compiler would treat both cases so I compiled the following code using Java 1.6:
public class IfTest
{
public static void main(String[] args)
{
IfTest iffy = new IfTest();
iffy.doit();
}
private void doit()
{
Random rand = new Random();
boolean a = rand.nextBoolean();
boolean b = rand.nextBoolean();
boolean c = rand.nextBoolean();
if (a && b && c)
{
System.out.println("block 1");
}
if (a)
{
if (b)
{
if (c)
{
System.out.println("block 2");
}
}
}
}
}
...then decompiled it using Jad. Here's what the decompiler produced from the class file:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name: IfTest.java
import java.util.Random;
public class IfTest
{
public IfTest()
{
}
public static void main(String args[])
{
IfTest iffy = new IfTest();
iffy.doit();
}
private void doit()
{
Random rand = new Random();
boolean a = rand.nextBoolean();
boolean b = rand.nextBoolean();
boolean c = rand.nextBoolean();
if(a && b && c)
System.out.println("block 1");
if(a && b && c)
System.out.println("block 2");
}
}
I guess it doesn't matter which way you write it.
Upvotes: 5
Reputation: 500357
In Java, &&
and ||
are guaranteed to short-circuit: the operands are evaluated left-to-right, and the evaluation stops as soon as the result is known with certainty.
From the JLS:
The
&&
operator is like&
(§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand istrue
. It is syntactically left-associative (it groups left-to-right).The
||
operator is like|
(§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is false. It is syntactically left-associative (it groups left-to-right).
This means that the two code snippets in your question are exactly equivalent.
Upvotes: 17
Reputation: 2673
&& and || are short-circuit logical operators. It means that in your example, if a is false, then the rest of the expression won't be evaluated.
If you want non-short-circuit logical operators, you can use & and |.
You can look at the following statement of the Java Language Specification if you need confirmation: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.2
Upvotes: 1
Reputation: 3045
This:
if (condition is met) {
//continute to next check
if (condition is met) {
//continue to next check
if (condition is met) {
do stuff }
}
}
Does the same as: if (a && b && c) { do stuff }
, but takes up ALOT of space. You don't really have to worry about efficiency with this particular code segment, so going with your first option is your best bet. You could also do
if (a && b){
if (condition is met) {
do stuff }
}
Upvotes: 1
Reputation: 51030
if (a && b && c) { do stuff }
That has short circuiting. &&
will short-circuit if a
evaluates to false, meaning rest of checking is not done.
Other than that, I think there is no difference between the two approaches.
Upvotes: 1
Reputation: 62439
In the first case, the if
evaluates to false
on the first encountered false
condition (it short-circuits). Otherwise you would not be able to do something like:
if(a != null && a.toString().equals("b"))
without getting a NullPointerException
if the second part would also be evaluated.
Upvotes: 2
Reputation: 1500525
The best way to check this sort of thing is to look in the Java Language Specification.
In this case, you want section 15.23:
At run time, the left-hand operand expression is evaluated first; if the result has type Boolean, it is subjected to unboxing conversion (§5.1.8); if the resulting value is false, the value of the conditional-and expression is false and the right-hand operand expression is not evaluated.
So no, in the expression a && b && c
, b
and c
are not evaluated if a
evalutes to false
.
Note that this isn't part of the if
statement behaviour - it's part of the &&
operator behaviour. You can see the same effect just using the expression elsewhere:
// Still won't evaluate b and c if a is false...
boolean x = a && b && c;
(It's worth being aware of what behaviour is due to which part of the language for when you want to consult the specification.)
Upvotes: 4
Reputation: 340733
These code snippets are completely equivalent and produce exactly the same bytecode:
0: iload_1
1: ifeq 20 //a == false (0)
4: iload_2
5: ifeq 20 //b == false (0)
8: iload_3
9: ifeq 20 //c == false (0)
12: //do stuff
20: return
Although the explanation is different, the code generator produces the same output. In the first case due to lazy evaluation subsequent terms aren't evaluated if previous one is false
. In the second case the runtime won't go deeper if surrounding if
condition is not met.
Upvotes: 6
Reputation: 120198
for (a && b && c)
, the conditional "short circuits", meaning as soon as one fails, the rest are not evaluated.
However, unless getting a
, b
, and c
is somehow expensive, this could be a case of premature optimization.
If some or all are expensive, then if you do
if (getA() && getB() && getC())...
then if getA()
fails, the other methods aren't even called.
Upvotes: 5