Monday 15 August 2011

logic - Inconsistencies with && and divide-by-zero error in Java -


I am starting Java, and I have to face some discrepancies why this works:

  if ((D! = 0) & amp; (n / d & lt; 3)) {count (A, D); }   

But if I do this:

  if ((n / d & lt; 3) & amp; amp; (D! = 0)) {count (A, D); }   

I get this error:

  Exception in thread "main" java.lang.ArithmeticException: / compute.main at zero (compute.java : 63) Java   

Evaluates expressions using Java, this means that & Amp; Amp; For , if the left expression is false , then Java does not bother to evaluate the correct expression (since the full expression is guaranteed false) already

In your first example, if d == 0 , then d! = 0 is incorrect, so it's n / d . In your second example, if d == 0 , then n / d & lt; 3 will divide you by zero error.

No comments:

Post a Comment