In Java, if I use a ternary if operator inside a regular if, for example:
if ((x > y - z) ? true : callLongWaitedMethod(many, parameteres)) {
loveTeddyBear();
}
will it execute the callLongWaitedMethod if x > y - z is indeed true? I hope it is not and I can use this nice statement, slightly complicated at the first glance, but more attractive to compare with the extra boolean variable:
boolean b = (x > y - z) ? true : callLongWaitedMethod(many, parameteres);
if (b) {
loveTeddyBear();
}
especially if I’m using this inside a big loop which iterates over and over, so creating boolean each time will not be nice from the performance point of view while if I declare the boolean outside the loop, I may miss the neat because of the big size of the loop.
callLongWaitedMethodwill not be called ifx > y - zis true.