• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

operators

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from K & B book page 417:

Would someone explain to me how this breaks down, that is, how to approach solving it:

(x > 4 && x < 8)

Thanks.

Matt

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
&& says -

false && false -> false
false && true -> false
true && false -> false
true && true ->



So, in this case it would return true when x is bigger than 4 and lower than 8, false otherwise.

Regards,
Dan
 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan.

So, it sounds like your saying (x > 4 && x < 8) is the same as ((x > 4) && (x < 8))?

Without parentheses it would seem to me if you evaluate left to right, you'd evaluate "x > 4", let's say true, then evaluate "true && x" where x is some int and you'd get a compiler error because an int does not resolve to a boolean. Where do I go haywire?

Thanks.

Matt

 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matt, it's about Operator Precedence.

< and > take precedence over &&.

Regards,
Dan
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's an order of precedence of operators in Java (and most other languages). Operators with higher precedence get applied first regardless of the order they appear - unless you use brackets to force a particular order.

For a possibly more familiar example, what would you expect (1 + 2*3) to be? The * has higher precedence than +, so it's 7, not 9.

Another example: = is actually an operator. If it wasn't for the rules of precedence, something as straightforward as x = a + 1 really wouldn't do what you'd expect (think about what the effect is if you applied that left-to-right).

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html for the full rules of precedence.

As a rule, though, when writing code if you have any confusion as to what the order will be, add brackets to make it clearer, even if they aren't strictly needed.
reply
    Bookmark Topic Watch Topic
  • New Topic