Enthält Java überprüfen Sie alle Argumente in „&&“ (und) Operator, selbst wenn einer von ihnen falsch ist?

StackOverflow https://stackoverflow.com/questions/4163742

Frage

Ich habe einen solchen Code:

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

Angenommen, Objekt null ist.

Ist dieses Codeergebnis in Nullpointer oder if-Anweisung nicht ausgeführt wird?

Wenn ja, wie dieser Code Refactoring eleganter sein (wenn es natürlich möglich ist)?

War es hilfreich?

Lösung

Java hat Kurzschluss Auswertung haben, das heißt der Code sollte in Ordnung sein

Andere Tipps

&& tut Kurzschluss während & würde nicht.

Aber mit einfachen Fragen wie diese, ist es am besten, nur um es zu versuchen (ideone helfen kann, wenn Sie keinen Zugang zu einem Computer haben).

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

Schließlich ist der Ort, um Check sicher wäre die JLS §15.23 . Nicht die einfache Sache zu lesen, die relevent Abschnitt heißt es: Der Operator && ist wie & ( §15.22. 2 ), sondern wertet seine rechten Operanden nur dann, wenn der Wert seiner linken Operanden wahr ist.

Die beste Weg, es versuchen würde, um herauszufinden, vor allem für eine einzelne Zeile Frage. Wäre schneller gewesen, auch.

Die Antwort ist, dass Java nicht den Körper des „if“ ausführen.

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.

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".

Maybe this other question helps you:

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

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.

&& 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

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

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top