short-circuit operators - part of the spec?

Discussion in 'Java' started by Phil..., Oct 22, 2003.

  1. Phil...

    Phil... Guest

    My book says the following about && and ||
    (see below dashed line.) And I wonder if this
    is built into the language or an artifact of the
    compiler. Previously (in my youth) we were
    taught that depending on the compiler to skip
    evaluating stuff was dangerous and bad for
    maintenance.
    ----------------------------
    known as the short-circuit logical operators:
    && short-circuit AND
    || short-circuit OR
    The short-circuit feature of the && operator is that it doesn't waste its
    time on
    pointless evaluations. A short-circuit && evaluates the left side of the
    operation first
    (operand one), and if operand one resolves to false, the && operator doesn't
    bother
    looking at the right side of the equation (operand two). The operator
    already knows
    that the complete expression can't possibly be true, since one operand has
    already
    proven to be false.
    Phil..., Oct 22, 2003
    #1
    1. Advertising

  2. Phil... <> coughed up the following:

    > My book says the following about && and ||
    > (see below dashed line.) And I wonder if this
    > is built into the language or an artifact of the
    > compiler. Previously (in my youth) we were
    > taught that depending on the compiler to skip
    > evaluating stuff was dangerous and bad for
    > maintenance.


    Depends upon the language, and no, there is nothing wrong with it when it is
    specifically allowed.

    In your youth, if you used C or C++, you have the same "short circuit"
    evaluation leap that you have in java.

    What /is/ a bad idea, which is probably what you're confusing this with, is
    to rely upon a feature that happens to be present in a compiler but isn't
    specified in the language spec. That /is/ dangerous.
    Thomas G. Marshall, Oct 22, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil...
    Replies:
    10
    Views:
    729
    Joona I Palaste
    Oct 23, 2003
  2. JS#
    Replies:
    5
    Views:
    3,532
    Roedy Green
    Nov 21, 2005
  3. Stefanie Ertheld

    short circuit operators

    Stefanie Ertheld, Feb 24, 2008, in forum: Java
    Replies:
    26
    Views:
    1,697
  4. Anthony Paul

    short-circuit evaluation and assignment operators

    Anthony Paul, Jun 6, 2009, in forum: C Programming
    Replies:
    5
    Views:
    1,287
  5. Andrew Chen
    Replies:
    1
    Views:
    190
    David Chelimsky
    Mar 25, 2008
Loading...

Share This Page