Associativity of unary operators

Discussion in 'Java' started by Ashwin N, Sep 8, 2006.

  1. Ashwin  N

    Ashwin N Guest

    Hi all,

    I know this is actually a language independent question, but I came
    across it while studying Java, so well ...

    What exactly is the use of specifying an associativity for unary
    operators? I do know how associativity is related to precedence and I
    know how it is important for "binary" operators.

    For example, the Java unary operators ++, --, ~, ! all have the same
    precedence and have a "right-to-left" associativity. Can anyone give me
    an example where this unary operator associativity is useful and is
    applied?

    PS: Are there other newsgroups where this query might be more apt? If
    yes, do let me know.

    TIA,
    ~ash
    --
    Ashwin Nanjappa
    http://ashwin.zaatar.org/
     
    Ashwin N, Sep 8, 2006
    #1
    1. Advertising

  2. Ashwin N wrote:
    > Hi all,
    >
    > I know this is actually a language independent question, but I came
    > across it while studying Java, so well ...
    >
    > What exactly is the use of specifying an associativity for unary
    > operators? I do know how associativity is related to precedence and I
    > know how it is important for "binary" operators.
    >
    > For example, the Java unary operators ++, --, ~, ! all have the same
    > precedence and have a "right-to-left" associativity. Can anyone give me
    > an example where this unary operator associativity is useful and is
    > applied?


    Section 15.15 Unary Operators in the JLS does not mention
    "associativity" but just says "Expressions with unary operators group
    right-to-left, so that -~x means the same as -(~x)"
    [http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4990]

    That rule allows stacking of unary operators without requiring parentheses.

    Is there some other place you are thinking of, that does talk about
    associativity for unary operators? Could you give a reference?

    Patricia
     
    Patricia Shanahan, Sep 8, 2006
    #2
    1. Advertising

  3. Ashwin  N

    ash Guest

    Patricia Shanahan wrote:
    [...]
    >
    > Section 15.15 Unary Operators in the JLS does not mention
    > "associativity" but just says "Expressions with unary operators group
    > right-to-left, so that -~x means the same as -(~x)"
    > [http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4990]
    >
    > That rule allows stacking of unary operators without requiring
    > parentheses.


    Thanks for this pointer! Can I conclude that unary operators in Java
    have *no* associativity but just a right-to-left "grouping"?

    > Is there some other place you are thinking of, that does talk about
    > associativity for unary operators? Could you give a reference?


    Yes. I came across this in Table 5.3 (Page 241) of the book _An
    Introduction to Object-Oriented Programming with Java_ by C. Thomas Wu
    (4 Ed).

    Also, most online Java references which have an operator precedence
    table slap a right-to-left "associativity" on the unary operators. For
    example:
    <http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html>

    Thanks,
    ~ash
    --
    Ashwin Nanjappa
    http://ashwin.zaatar.org/
     
    ash, Sep 9, 2006
    #3
  4. ash wrote:
    > Patricia Shanahan wrote:
    > [...]
    >> Section 15.15 Unary Operators in the JLS does not mention
    >> "associativity" but just says "Expressions with unary operators group
    >> right-to-left, so that -~x means the same as -(~x)"
    >> [http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4990]
    >>
    >> That rule allows stacking of unary operators without requiring
    >> parentheses.

    >
    > Thanks for this pointer! Can I conclude that unary operators in Java
    > have *no* associativity but just a right-to-left "grouping"?


    I was cautious about saying that, because I don't have the entire JLS
    memorized.

    The JLS does use the term "associative", but in its mathematical sense,
    as the property that, for a binary operation "op", "(a op b) op c" is
    equal to "a op (b op c)".

    For example, "Addition is a commutative operation if the operand
    expressions have no side effects. Integer addition is associative when
    the operands are all of the same type, but floating-point addition is
    not associative." in 15.18.2 Additive Operators (+ and -) for Numeric Types.

    >
    >> Is there some other place you are thinking of, that does talk about
    >> associativity for unary operators? Could you give a reference?

    >
    > Yes. I came across this in Table 5.3 (Page 241) of the book _An
    > Introduction to Object-Oriented Programming with Java_ by C. Thomas Wu
    > (4 Ed).
    >
    > Also, most online Java references which have an operator precedence
    > table slap a right-to-left "associativity" on the unary operators. For
    > example:
    > <http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html>


    Perhaps try writing to the author of one of those sources asking what
    they mean by "associativity" when applied to unary operators? Even for
    binary operations, the on-line reference seems to be using
    "associativity" as somehow relating to order of evaluation, rather than
    as meaning that order of evaluation does not matter.

    Patricia
     
    Patricia Shanahan, Sep 9, 2006
    #4
  5. Ashwin  N

    Stefan Ram Guest

    Stefan Ram, Sep 9, 2006
    #5
    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. Ruben Campos
    Replies:
    5
    Views:
    559
    Ruben Campos
    Nov 30, 2004
  2. Replies:
    2
    Views:
    311
    Andrey Tarasevich
    Oct 25, 2006
  3. dspfun

    Associativity of unary C Operators

    dspfun, Jan 1, 2007, in forum: C Programming
    Replies:
    28
    Views:
    889
    CBFalconer
    Jan 5, 2007
  4. SpOiLeR
    Replies:
    10
    Views:
    813
    SpOiLeR
    Oct 19, 2005
  5. , India

    associativity of operators

    , India, Mar 16, 2007, in forum: C Programming
    Replies:
    8
    Views:
    326
    Richard Harter
    Mar 18, 2007
Loading...

Share This Page