• 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

Logical Operators Involving Precedence and Unary Operators

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am preparing for an exam and I am trying to figure out why the output of this code snippet is:
true
3


This involves logical operators precedence and unary operators.



Thanks in advance!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Uy wrote:
I am preparing for an exam and I am trying to figure out why the output of this code snippet is:
true
3


This involves logical operators precedence and unary operators.




It would help us a bit, if you tell us what you were expecting -- and why?

BTW, the answer is pretty complicated*. Besides, as you mentioned, the requirement of an understanding of precedence and unary operators, it also requires an understanding of associativity, order of evaluation, and the short circuit capability of the logical operators.

Henry

* Okay, perhaps complicated is the wrong word here. It is more like there are a lot of stuff going on that needs to be accounted for.
 
Matt Uy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Sorry for the vague post. I am trying to prepare for the OCAJP certification and noticed that I am in the wrong part of the forum. My bad.

I was just practicing some logical operators and came up with this code snippet. I'm just trying to understand more about logical operators with its precedence and short circuit along with the unary operators all combined. I tried looking for a same code snippet in the internet but could not find any.

I have a little knowledge about the unary operators, short circuit and precedence of logical operators but I would really appreciate for some explanation on what was evaluated, where the short circuit happened and other things here in the code.

Thank you,
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Uy wrote:
I was just practicing some logical operators and came up with this code snippet. I'm just trying to understand more about logical operators with its precedence and short circuit along with the unary operators all combined. I tried looking for a same code snippet in the internet but could not find any.

I have a little knowledge about the unary operators, short circuit and precedence of logical operators but I would really appreciate for some explanation on what was evaluated, where the short circuit happened and other things here in the code.



There are way too many thing going on in this example for it to be useful, IMO.

How about this? Why don't you tell us what you know about each concept, and we can get you up to speed on the components first, before we put everything together.

For example, what do you think is the precedence and only the precedence? And not worry about associative, order of evaluation, the unary pre/postfix operators, or the short circuit of the logic operators yet. Once you get that right, we can move on.

Henry
 
Matt Uy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How about this? Why don't you tell us what you know about each concept, and we can get you up to speed on the components first, before we put everything together.

For example, what do you think is the precedence and only the precedence? And not worry about associative, order of evaluation, the unary pre/postfix operators, or the short circuit of the logic operators yet. Once you get that right, we can move on.



Hi Henry, thank you for the reply.

For the precedence of the logical operators, I know that the && operator is higher than the || operator.
And what I know about the short circuit is that:
  • for && operator if it evaluated the first expression and the result is false, it will no longer evaluate the second expression. Example: (5 < 1 && 2 >1)
  • for the || operator if it evaluated the first expression and the result is true, it will no longer evaluate the second expression. Example: (10 > 1 || 10 >5)


  • What I would like to know is how will it evaluate something like this: (5 > 1 || 5 < 10 && 5 > 1).
  • Will the (5 < 10 && 5 > 1) be evaluated first?
  • And will it still evaluate (5 > 1) considering (5 < 10 && 5 > 1) resulted to true? Since after evaluating the && operator first, (5 > 1 || 5 < 10 && 5 > 1) will now result to (5 > 1 || true).


  • Then how about this expression (1 < 5 || 6 > 1 && 4 > 1 || 4 > 2 || 5 < 1 && 10 > 2)?
  • Will (6 >1 && 4 > 1) and (5 < 1 && 10 > 2) be evaluated first?
  • And will (5 < 1 && 10 > 2) still be evaluated considering that (6 >1 && 4 > 1) resulted to true?




  • Thanks,
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Matt Uy wrote:
    What I would like to know is how will it evaluate something like this: (5 > 1 || 5 < 10 && 5 > 1).

  • Will the (5 < 10 && 5 > 1) be evaluated first?
  • And will it still evaluate (5 > 1) considering (5 < 10 && 5 > 1) resulted to true? Since after evaluating the && operator first, (5 > 1 || 5 < 10 && 5 > 1) will now result to (5 > 1 || true).



  • No. Order of evaluation is from left to right (see section 15.7 of the Java Language Specification... http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7).

    Remember that precedence and evaluation order are two different things -- please don't mix them up.

    Henry
     
    Matt Uy
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay, much thanks for the info and the link!

    thanks,
     
    reply
      Bookmark Topic Watch Topic
    • New Topic