Sign in to follow this  
X Abstract X

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();

if (!myFile.load() || !myFile.read() || !myFile.close()) {
   return(false);
}


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

Share this post


Link to post
Share on other sites
Halifax2    295
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
mightshade    100
Quote:
Original post by Halifax2
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.

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 this post


Link to post
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 this post


Link to post
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 throw
try {
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
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this