This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

evaluating logical short-circuit operators ???

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Swan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers rikko
 
That new kid is a freak. Show him this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic