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.
, Java ( ) lazy evaluation, , .
, :
if(p != null && p.getAge() > 10)
, a || b b, a true.
a || b
b
a
true
Java , bool2 bool2, bool1 false?
, , . .
if(s != null && s.length() > 0)
if(s == null || s.length() == 0)
, & |, .
&
|
, & && Java ( | ||).
&&
||
& | , && || , ,
if(bool1 && bool2 && bool3) {
bool2 bool3, bool1 ,
bool2
bool3
bool1
if(bool1 & bool2 & bool3) {
.
boolean foo() { System.out.println("foo"); return true; }
if(foo() | foo()) foo , if(foo() || foo()) - .
if(foo() | foo())
foo
if(foo() || foo())
, .
,