Java Logical "or" Operator

Recommended Posts

X Abstract X    109
EDIT: Found the answers in the Java specification. The operands are in fact evaluated from left to right. Hello, I have a question concering the || operator in Java. For example, I have a File object with the methods load(), read(), close(). In this case, the order of the methods performed is important; obviously you can't read the file before opening it. My question is, if I use the following code, are the methods guranteed to be called in the order that I list them?
File myFile = new File();

return(false);
}


[Edited by - X Abstract X on February 13, 2010 11:54:11 PM]

Share on other sites
KratosDon    112
Yes. Also, if one of them returns true, the subsequent ones won't be called.

Share on other sites
Halifax2    295
Quote:
 Original post by KratosDonYes. Also, if one of them returns true, the subsequent ones won't be called.

Actually, if one of the methods returns false the subsequent calls won't be made. This operates exactly how he expects if the calls truly are made in order. I don't know whether Java guarantees that; it's not really necessary that they would guarantee it since they could do some short-circuit optimizations. Maybe someone with more Java experience can way in.

Really I don't know why you would bother writing code like that. I would opt for the solution that is most readable and doesn't rely on programmers having to guess whether the calls are made in order or not.

Share on other sites
X Abstract X    109
Thanks for posts guys. I'm actually not writing my code like this. I was thinking about it but opted for longer code with better readability. I just posted this to satisfy my curiousity.

Share on other sites
Halifax2    295
Okay, I did a little reading up on the topic to satisfy my own curiosity. I read the Java documentation on expression evaluation, and came up with this:
Quote:
 The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

Share on other sites
X Abstract X    109
Thanks again. Here's what I found.

"At run time, the left-hand operand expression is evaluated first..." [http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.24]

Share on other sites
Quote:
Original post by Halifax2
Quote:
 Original post by KratosDonYes. Also, if one of them returns true, the subsequent ones won't be called.

Actually, if one of the methods returns false the subsequent calls won't be made.

The documentation you later linked to says something else:
Quote:
 15.24 Conditional-Or Operator ||(...)If the value of the left-hand operand is false, then the right-hand expression is evaluated and its value becomes the value of the conditional-or expression.

Are you perhaps confusing this with the &&-operator, where the result of the entire expression evaluates to false if at least one of the operands' value is false?

Share on other sites
Halifax2    295
No I'm not mistaken at all, "...if one of the methods returns false the subsequent calls won't be made," corroborates what you wrote above. You are not taking into account the fact that his expression inverts what all three methods return, thus false, returned from the method, becomes true, thus creating a short-circuit in the expression.

Share on other sites
Fenrisulvur    186
So, two things:
-You're arguing over a technicality; and
-Using inversions and logical ors instead of logical ands should probably be discouraged.

Share on other sites
Antheus    2409
The original example is idiomatic PHP or C.

Idiomatic Java would be something like this:
final File f = new File(); // doesn't throwtry {  f.load(filename);  f.read();} catch (Exception e) {  // something...} finally {  f.close();}

Idiomatic C++ would be something like this:
void foo() {  File f(filename);  f.read();}try {  foo();} catch (.../*something*/) {  // something}