Search code examples
javalogical-operatorsshort-circuitingand-operator

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


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)?


Solution

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