This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
 
subject: evaluating logical short-circuit operators ???
 
Similar Threads
Need help with if/else issue....
Short Circuit operators and Bitwise operators
How does this code works ?
K & B boolean Question
EL Query