File myFile = new File();
if (!myFile.load() || !myFile.read() || !myFile.close()) {
return(false);
}
Java Logical "or" Operator
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?
[Edited by - X Abstract X on February 13, 2010 11:54:11 PM]
Quote:Original post by KratosDon
Yes. 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.
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.
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.
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]
"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]
Quote:Original post by Halifax2Quote:Original post by KratosDon
Yes. 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?
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.
So, two things:
-You're arguing over a technicality; and
-Using inversions and logical ors instead of logical ands should probably be discouraged.
-You're arguing over a technicality; and
-Using inversions and logical ors instead of logical ands should probably be discouraged.
The original example is idiomatic PHP or C.
Idiomatic Java would be something like this:
Idiomatic C++ would be something like this:
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}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement