File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes evaluating logical short-circuit operators ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "evaluating logical short-circuit operators ???" Watch "evaluating logical short-circuit operators ???" New topic
Author

evaluating logical short-circuit operators ???

David Swan
Ranch Hand

Joined: Oct 08, 2003
Posts: 33
Can anyone tell me how to evaluate this line in the following code: if (true && false | true)
should you start evaluating from left to right ?? and where does the compiler put parentheses into this statement. Heres the full code, "hi" is output to the screen.
class test {
public static void main(String[] args) {
if (true && false | true)
System.out.println("hi");
}
}
Thanks
Dave


SCJP, SCWCD, SCBCD
David Swan
Ranch Hand

Joined: Oct 08, 2003
Posts: 33
Also here, which && operator works on which boolean values???
class test {
public static void main(String[] args) {
if (true && true && false)
System.out.println("hi");
}
}
Cheers
Dave
Rikko Verrijzer
Ranch Hand

Joined: Jul 22, 2003
Posts: 34
Hi,
The precedence of these operators is this:
&
^
|
&&
||
This means in your case that first | is evaluated on both values because it isn't a shortcut operator and then with the result of the previous && is evaluated. In your case you would get something like this (true && (false|true))
hope this helps?
Rikko
David Swan
Ranch Hand

Joined: Oct 08, 2003
Posts: 33
Cheers rikko
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: evaluating logical short-circuit operators ???
 
Similar Threads
EL Query
K & B boolean Question
Need help with if/else issue....
Short Circuit operators and Bitwise operators
How does this code works ?