How Java handles multiple conditions inside a single IF statement

Suppose I have this:

if(bool1 && bool2 && bool3) {
...
}

Now. Is Java smart enough to skip checking bool2 and bool2 if bool1 was evaluated as false? Can Java check them from left to right? I ask about this because I "sorted" the conditions inside my own, if by the time it is needed (starting from the cheapest on the left). Now I'm not sure if this gives me any performance advantages because I don’t know how it handles Java.

+5
source share
4 answers

, Java ( ) lazy evaluation, , .

, :

if(p != null && p.getAge() > 10)

, a || b b, a true.

+19

Java , bool2 bool2, bool1 false?

, , . .

if(s != null && s.length() > 0)

if(s == null || s.length() == 0)

, & |, .

+12

, & && Java ( | ||).

& | , && || , ,

if(bool1 && bool2 && bool3) {

bool2 bool3, bool1 ,

if(bool1 & bool2 & bool3) {

.

, :

boolean foo() {
    System.out.println("foo");
    return true;
}

if(foo() | foo()) foo , if(foo() || foo()) - .

+6

, .

,

+4
source

All Articles