0

Will this code not evaluate the divide by zero portion of the if statement since the first part evaluates to false? If so, is this true for all cases in all Java IDEs? Or will certain compilers throw the exception?

int n = 0;
int x = 5;
if (n != 0 && x / n > 100) {
    System.out.println(" s1");
} else {
    System.out.println("s2");
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3328187
  • 193
  • 2
  • 2
  • 6
  • 2
    All (correct) Java compilers and JVMs will run it the same way. As for what it'll do... why not just give it a try and see for yourself? – yshavit Apr 22 '15 at 15:24
  • Java uses [short circuit evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation) for boolean operations. Only as much as required is evaluated. This is **mandated** by [JLS 15.23](https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.23). – Boris the Spider Apr 22 '15 at 15:25
  • Oops, closed this for the wrong dupe reason. Let me try and find a better one... – yshavit Apr 22 '15 at 15:26
  • @BoristheSpider I don't think this is a short-circuit evaluation problem. It would be if you declare `final int n = 0;`, but this isn't the case. – Luiggi Mendoza Apr 22 '15 at 15:26
  • 1
    @LuiggiMendoza I don't get you: `if(a && b)` will not evaluate the expression `b` is `a == false`. – Boris the Spider Apr 22 '15 at 15:27
  • Bah, reopened by mistake. This should probably be closed as dup of http://stackoverflow.com/questions/8759868/java-logical-operator-short-circuiting, but I can't vote to close it because I reopened it. – yshavit Apr 22 '15 at 15:28
  • @BoristheSpider I'm assuming other things around the code. But if that piece of code is inside a single method, then this falls into short-circuit evaluation. – Luiggi Mendoza Apr 22 '15 at 15:30

2 Answers2

1

From JLS §15.23:

The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

So no, you will not get an exception.

Obviously this assumes that you have a single-threaded or thread safe environment - if you introduce visibility problems into the mix then it's anyone's guess.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

Assuming we include the import for SoP, and that thoses lines are written inside a static main, there will be no exception thrown, and s2 will be printed. X/0 will never be evaluated in your code because && check left expression first, thus, it will never throw anything.

Mekap
  • 2,065
  • 14
  • 26
  • 1
    I disagree. If `int n` is declared as field and not as local parameter and this part of the code is executed by multiple threads and one of the threads assigns `n` with `0`, then the exception may be thrown. – Luiggi Mendoza Apr 22 '15 at 15:28
  • "IF". Exactly, i'm just basing my answer of the code Op gave. In any java environnement, thoses lines of code will never throw an exception. Assuming the import of SoP is given of course. I don't recall op talking about multihreading – Mekap Apr 22 '15 at 15:31
  • We cannot be sure of that. Otherwise, the code would be posted inside a method. We can assume anything, so the post could also be closed as too broad. – Luiggi Mendoza Apr 22 '15 at 15:31
  • Stricto-sensu you're right. But i assumed in my answer that 1. the import is given and 2. This code is written inside a main & a class. I'll correct my answer to include thoses 2 assumptions. Thanks for pointing out the gray area Luiggi – Mekap Apr 22 '15 at 15:34