Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Associativity of unary operators

Thread view: 
Ashwin  N - 08 Sep 2006 04:15 GMT
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
Signature

Ashwin Nanjappa
http://ashwin.zaatar.org/

Patricia Shanahan - 08 Sep 2006 06:18 GMT
Ashwin N wrote:
> Hi all,
>
[quoted text clipped - 9 lines]
> 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
ash - 09 Sep 2006 07:03 GMT
[...]

> Section 15.15 Unary Operators in the JLS does not mention
> "associativity" but just says "Expressions with unary operators group
[quoted text clipped - 3 lines]
> 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
Signature

Ashwin Nanjappa
http://ashwin.zaatar.org/

Patricia Shanahan - 09 Sep 2006 13:41 GMT
> [...]
>> Section 15.15 Unary Operators in the JLS does not mention
[quoted text clipped - 7 lines]
> 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?
[quoted text clipped - 7 lines]
> 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
Stefan Ram - 09 Sep 2006 13:59 GMT
>What exactly is the use of specifying an associativity for unary
>operators?

 left:  --x = (--)x
 right: --x - -(-x)

 according to the bottom of page 297 (PDF-page 313) of

http://www.cs.york.ac.uk/ftpdir/reports/YCS-2004-383.pdf


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.