Does Java check all arguments in "&&" (and) operator even if one of them is false?
Asked Answered
D

10

14

I have such code:

if(object != null && object.field != null){
    object.field = "foo";
}

Assume that object is null.

Does this code result in nullPointerException or just if statement won't be executed?

If it does, how to refactor this code to be more elegant (if it is possible of course)?

Denticle answered 12/11, 2010 at 10:36 Comment(2)
Play around: ideone.com/KRMwjDryly
And the other case: ideone.com/NAP0QDryly
R
10

Java does have short circuit evaluation, i.e. your code should be ok

Rellia answered 12/11, 2010 at 10:40 Comment(3)
Java has short-circuiting, true, but it's only used in this example because of the use of && as opposed to &. & would result in a NullPointerException.Audient
"&& as opposed to &"? Where in the question do you see that?Rellia
Nowhere, I was just adding that point because the & operator does not have short-circuiting.Audient
C
19

&& does short circuit while & would not.

But with simple questions like this, it is best to just try it (ideone can help when you don't have access to a machine).

&& - http://ideone.com/LvV6w & - http://ideone.com/X5PdU

Finally the place to check for sure would be the JLS §15.23. Not the most easy thing to read, the relevent section states: The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

Coeducation answered 12/11, 2010 at 10:40 Comment(0)
R
10

Java does have short circuit evaluation, i.e. your code should be ok

Rellia answered 12/11, 2010 at 10:40 Comment(3)
Java has short-circuiting, true, but it's only used in this example because of the use of && as opposed to &. & would result in a NullPointerException.Audient
"&& as opposed to &"? Where in the question do you see that?Rellia
Nowhere, I was just adding that point because the & operator does not have short-circuiting.Audient
V
4

One way to know it! Test it! How? Well, make a method which prints out something:

public static boolean test(int i)
{
    System.out.println(i);
    return false;
}

...

if (test(1) && test(2) && test(3))
{
    // not reached
}

This prints:

1

So the answer on your question is "no".

Volatile answered 12/11, 2010 at 10:50 Comment(0)
E
2

Best way to find out would be try it, especially for a single line question. Would have been faster, too.

The answer is that Java will not execute the body of the "if".

Eldwen answered 12/11, 2010 at 10:40 Comment(2)
True, but "proof by experimentation" isn't always the best approach. Think about testing concurrency-related stuff on a single-core machine, for example. It's always good to ask and understand the underlying mechanics rather than hoping your tests include all the edge cases for the black box...Fluorspar
Remember, we're talking about one line of code here. "Edge cases"? Not in this case.Eldwen
C
2

This will not throw any NullPointerException . The condition will be evaluated from left to right and the moment first false expression is found it will not evaluate remaining expression.

Calcutta answered 12/11, 2010 at 10:41 Comment(0)
P
2

Maybe this other question helps you:

Differences in boolean operators: & vs && and | vs ||

Potation answered 12/11, 2010 at 13:31 Comment(0)
S
1

Java has short circuit evaluation, so it will be fine.

The code looks ok to me, but do you actually need to check object.field != null? I think that test can be omitted as you never use the variable, just set it.

On a side-note, most programmers wouldn't access fields directly (object.field) but rather through getters/setters (object.setField(x);). Without any more context to go on, I can't say if this is appropriate in your case.

Sherl answered 12/11, 2010 at 10:41 Comment(0)
C
1

&& and || conditions stops at the point they can decide whether the condition is true/false, in your case, the condition will stop right after object != null and I think that your code is just fine for this case

Cerecloth answered 12/11, 2010 at 10:43 Comment(0)
R
0

If you want all of your boolean expressions evaluated regardless of the truth value of each, then you can use & and | instead of && and ||. However make sure you use these only on boolean expressions. Unlike && and ||, & and | also have a meaning for numeric types which is completely different from their meaning for booleans. http://ibiblio.org/java/course/week2/46.html

Rabjohn answered 12/11, 2010 at 10:42 Comment(0)
R
0

Although short circuiting would work here, its not a guarantee that (like I have done many times) you'll get the order wrong when writing another, it would be better practice to nest those if statements and define the order you want the boolean checks to break:

if(object != null) 
{
    if(object.field != null)
    {
        object.field = "foo";
    }
}

This does exactly the same as you're essentially saying, if the first boolean check fails don't do the second; it is also nullPointerException safe as object.field will not be checked unless object is not null

Using short-circuiting on booleans can become annoying later on as when you have a multiple bool if statement it becomes trickier to efficiently debug which part short circuited.

Ramification answered 12/11, 2010 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.