Explain the Order of Evaluation of Logic Operators ?
When Java sees a && operator or a ||, the expression on the left side of the operator is evaluated first. For instance, consider the subsequent:
boolean b, c, d;
b = !(3 > 2); // b is false
c = !(2 > 3); // c is true
d = b && c; // d is false
While Java evaluates the expression d = b && c;, it first checks whether b is true. Here b is false, so b && c must be false regardless of while c is or is not true, so Java doesn't bother checking the value of c.
On the other hand while faced along with an || Java short circuits the evaluation as soon as it encounters a true value because the resulting expression must be true. This short circuit evaluation is less significant in Java than in C because in Java the operands of && and || must be booleans that are unlikely to have side effects that depend on whether or not they are evaluated. Still it's likely to force them. For example consider this code.
boolean b = (n == 0) || (m/n > 2);
Even if n is zero this line will never cause a division through zero, because the left hand side is always evaluated first. If n is zero then the left hand side is true and there's no required to evaluate the right hand side. Mathematically this forms sense because m/0 is in some sense infinite that is greater than two.
This isn't a perfect solution by since m may be 0 or it may be negative. If m is negative and n is zero then m/n is negative infinity that is less than two. And if m is also zero, then m/n is very undefined.
The proper solution at this point depends on your problem. Since real world quantities aren't infinite, while infinities start popping up within your programs, nine times out of ten it's a sign in which you've lost too much precision. The remaining times are commonly signals that you've left out some little factor in your physical model in which would erase the infinity.
Thus if there's a real chance your program will have a divide by zero error think carefully about what it means and how you should respond to it. If, upon reflection, you decide in which what you actually need to know is whether m/n is finite and greater than zero you should use a line like this
boolean b = (n != 0) && (m/n > 0);