Associativity of unary operators

A

Ashwin N

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
 
P

Patricia Shanahan

Ashwin said:
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
 
A

ash

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
 
P

Patricia Shanahan

ash said:
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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top