Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Looking for good table with all Java operators

48 views
Skip to first unread message

Robbo

unread,
Jan 24, 2010, 7:21:11 PM1/24/10
to
Hello,

I need to create complete table of Java operators
with their priorities and associations. In Sun's web
page there is such table but not complete (e.g. there
are no operators like (), .), there is only one sentence
about associations. I tried to create such a table
myself, but do not know if it is correct. Please,
check my table or provide me with some good
link where I may find good table.

Thx.

Kategoria Operator ��czno��
??? () [] . Left to right
Postfix expr++ epxr-- Right to left
Unary ++expr --expr Right to left
+expr -expr ! ~
Multiplicative * / % Left to right
Additive + - Left to right
Shift >> >>> << Left to right
Relational > >= < <= Left to right
instanceof
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= Right to left
/= %= >>=
<<= >>>= &=
^= |=
Comma , Left to right

Robbo


John B. Matthews

unread,
Jan 24, 2010, 9:45:58 PM1/24/10
to
In article <hjioae$jef$1...@atlantis.news.neostrada.pl>,
"Robbo" <nie...@yle.com> wrote:

> Hello,
>
> I need to create complete table of Java operators
> with their priorities and associations. In Sun's web
> page there is such table but not complete (e.g. there
> are no operators like (), .), there is only one sentence
> about associations. I tried to create such a table
> myself, but do not know if it is correct. Please,
> check my table or provide me with some good
> link where I may find good table.
>
> Thx.
>

> Kategoria Operator £±czno¶æ


> ??? () [] . Left to right
> Postfix expr++ epxr-- Right to left
> Unary ++expr --expr Right to left
> +expr -expr ! ~
> Multiplicative * / % Left to right
> Additive + - Left to right
> Shift >> >>> << Left to right
> Relational > >= < <= Left to right
> instanceof
> Equality == != Left to right
> Bitwise AND & Left to right
> Bitwise XOR ^ Left to right
> Bitwise OR | Left to right
> Logical AND && Left to right
> Logical OR || Left to right
> Conditional ?: Right to left
> Assignment = += -= *= Right to left
> /= %= >>=
> <<= >>>= &=
> ^= |=
> Comma , Left to right

Sun:
<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html>

Princeton:
<http://www.cs.princeton.edu/introcs/11precedence/>

University of West Florida:
<http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Lew

unread,
Jan 24, 2010, 10:36:41 PM1/24/10
to
Robbo wrote:
>> I need to create complete table of Java operators
>> with their priorities and associations. In Sun's web
>> page there is such table but not complete (e.g. there
>> are no operators like (), .), there is only one sentence

Those aren't operators.
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.11>
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.12>

>> about associations. I tried to create such a table

Do you mean "associativity"?

They're pretty darn easy to find.
<http://www.google.com/search?q=java+operator+precedence+table>
(The UWF link is top on the list, the Sun link is second, the Princeton link
fourth.)

--
Lew

Robbo

unread,
Jan 25, 2010, 7:22:09 AM1/25/10
to
> Those aren't operators.

If not, why tables of operators you provided me with include () [] and . ?

Look:

Robbo

unread,
Jan 25, 2010, 8:00:36 AM1/25/10
to
And the second thing...

Here http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.12
you may find information that:
"The following 37 tokens are the operators, formed from ASCII characters:
Operator: one of
= > < ! ~ ? :
== <= >= != && || ++ --
+ - * / & | ^ % <<
>> >>>
+= -= *= /= &= |= ^= %= <<=
>>= >>>="

There is no information about "new" which is also operator:
http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html
"The new keyword is a Java operator that creates the object."
In tables of operators
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
there is no information about "new".

So, as you can see, I need GOOD table of operators, not any table of
operators.


Patricia Shanahan

unread,
Jan 25, 2010, 8:47:52 AM1/25/10
to
Robbo wrote:
> And the second thing...
>
> Here http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.12
> you may find information that:
> "The following 37 tokens are the operators, formed from ASCII characters:
> Operator: one of
> = > < ! ~ ? :
> == <= >= != && || ++ --
> + - * / & | ^ % <<
>>> >>>
> += -= *= /= &= |= ^= %= <<=
>>> = >>>="

This table is correct by definition. Java is what the JLS says it is.

"new" is a keyword, not an operator. It is in the same category as "class".

> There is no information about "new" which is also operator:
> http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html
> "The new keyword is a Java operator that creates the object."

Tutorials are secondary documentation, not necessarily correct. If the
JLS and a tutorial disagree about an issue of Java language definition,
believe the JLS and ignore what the tutorial says.

> In tables of operators
> http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
> there is no information about "new".
>
> So, as you can see, I need GOOD table of operators, not any table of
> operators.

I would avoid the whole attempt to construct tables that cover
everything. Use tables for the things that can be summarized easily in
tables, but don't try to push everything into that model.

Note that some symbols have multiple roles in the syntax.

For example, a "(" it can be the start of a Parenthesized Expression,
part of the syntax for a Method Invocation Expression, or part of the
syntax, along with "new", for a Class Instance Creation Expression. "."
also has several uses.

Patricia

Lew

unread,
Jan 25, 2010, 10:01:34 AM1/25/10
to
Robbo wrote:
>> Those aren't operators.

Please attribute citations.

People's interpretations are not normative. The JLS defines the terms that
apply to Java.

Did you even read the definitions in the JLS to which I linked?

--
Lew

Roedy Green

unread,
Jan 25, 2010, 10:03:46 AM1/25/10
to
On Mon, 25 Jan 2010 01:21:11 +0100, "Robbo" <nie...@yle.com> wrote,
quoted or indirectly quoted someone who said :

>I need to create complete table of Java operators
>with their priorities and associations

see http://mindprod.com/jgloss/precedence.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
Don�t be discouraged by a failure. It can be a positive experience. Failure is, in a sense, the highway to success, inasmuch as every discovery of what is false leads us to seek earnestly after what is true, and every fresh experience points out some form of error which we shall afterwards carefully avoid.
~ John Keats (born: 1795-10-31 died: 1821-02-23 at age: 25)

Roedy Green

unread,
Jan 25, 2010, 10:17:47 AM1/25/10
to
On Mon, 25 Jan 2010 13:22:09 +0100, "Robbo" <nie...@yle.com> wrote,

quoted or indirectly quoted someone who said :

>


>If not, why tables of operators you provided me with include () [] and . ?

You could describe Java with a grammar that considered them as
operators with a precedence. They do fit into the precedence scheme
even if they don't usually cause any sort of calculation. (cast) does
sometime do a sort of computation.

Robbo

unread,
Jan 25, 2010, 10:46:44 AM1/25/10
to
Thanks for the answer. Well, this
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
seems to be complete table of operators in Java. The only problem
I see is lack of "cast operator".
Right?

Robbo


Joshua Cranmer

unread,
Jan 25, 2010, 11:25:57 AM1/25/10
to

Java calls it a "cast expression"; if you carefully read the JLS, you'll
notice it is at the same level of precedence as an unary operator.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Roedy Green

unread,
Jan 25, 2010, 11:51:34 AM1/25/10
to
On Mon, 25 Jan 2010 14:00:36 +0100, "Robbo" <nie...@yle.com> wrote,

quoted or indirectly quoted someone who said :

>There is no information about "new" which is also operator:

new does behave like an ordinary operator with precedence. It must be
immediately followed by a ClassName. It cannot occur in any other
context. I guess one way of looking at this is new has extremely high
precedence. It is in my table.


see http://mindprod.com/jgloss/precedence.html

My table also has some notes on each operator, MORE button links to a
detailed discussion of each operator, and examples of use. My table
is aimed at newbies, not language lawyers (compiler implementors).

I still need to write something on == and !=, and the += family.

Robbo

unread,
Jan 25, 2010, 3:31:31 PM1/25/10
to
> see http://mindprod.com/jgloss/precedence.html

Your table is good piece of work. Much better than many
incomplete tutorials which may be found all over the internet.

But, if I am right, a few hours ago there was no information
about "new". Probably you have added it not more than
a few hours ago.
Where did you find information that "new" is officially
operator in Java? I am really interested in this topic.
Patricia Shanahan (in this thread) has said that "new" is not an operator.
I have not found any information that would confirm
that "new" is an operator in JLS (3rd edition).

And the second thing. In your table there is (cast) operator
at precedence of 1 (it has the same priority as prefix and unary
operators). Also association is "Right".
If I am right (byte)++x means that ++ is computed before (byte)
-- both (cast) and prefix ++ have the same priority and association
is "Right".
And in (byte)x++, (byte) is first because of higher priority of cast
than postfix ++.
What do you think?

Robbo


Mike Schilling

unread,
Jan 25, 2010, 3:55:07 PM1/25/10
to
Robbo wrote:
>> see http://mindprod.com/jgloss/precedence.html
>
> Your table is good piece of work. Much better than many
> incomplete tutorials which may be found all over the internet.
>
> But, if I am right, a few hours ago there was no information
> about "new". Probably you have added it not more than
> a few hours ago.
> Where did you find information that "new" is officially
> operator in Java? I am really interested in this topic.
> Patricia Shanahan (in this thread) has said that "new" is not an
> operator. I have not found any information that would confirm
> that "new" is an operator in JLS (3rd edition).

"Operator" means different things to different people. A parser guy
would probably call "new" an operator; because it combines expressions
into a more complex expression and has a defined precedence. But the
"offical" list of operators is found at
http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.12
and doesn't include cast, new, or parentheses.

> And the second thing. In your table there is (cast) operator
> at precedence of 1 (it has the same priority as prefix and unary
> operators). Also association is "Right".
> If I am right (byte)++x means that ++ is computed before (byte)
> -- both (cast) and prefix ++ have the same priority and association
> is "Right".
> And in (byte)x++, (byte) is first because of higher priority of
> cast
> than postfix ++.
> What do you think?

Other way around. Postfix ++ has a higher precedence than the cast
does.


Robbo

unread,
Jan 25, 2010, 4:34:01 PM1/25/10
to
> Other way around. Postfix ++ has a higher precedence than the cast
> does.

It means there is mistake in Mr. Doedy Green's table
(http://mindprod.com/jgloss/precedence.html).
In this table prefix and postfix has the same priority -- probably
simple mistake with numbers of precedence.
But more serious mistake in his table is that prefix is before
postfix. According to
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
postfix goes first.

I really wish official table of Java operators with precedence
and associations.
In C++ situations is not better. E.g. here
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
there is information that postfix has precedence 2 and prefix has
precedence 3. In my Polish edition of Stroustrup's book
postfix and prefix has the same precedence.

Robbo


Lew

unread,
Jan 25, 2010, 5:06:09 PM1/25/10
to
Robbo wrote:
>> Your table is good piece of work. Much better than many
>> incomplete tutorials which may be found all over the internet.
>>
>> But, if I am right, a few hours ago there was no information
>> about "new". Probably you have added it not more than
>> a few hours ago.
>> Where did you find information that "new" is officially
>> operator in Java? I am really interested in this topic.
>

It is not. The keyword 'new' is officially not an operator in Java.

> > Patricia Shanahan (in this thread) has said that "new" is not an
> > operator. I have not found any information that would confirm
> > that "new" is an operator in JLS (3rd edition).
>

That's because 'new' is not an operator in the JLS (any edition), ergo
in the Java language.

As Patricia also pointed out, the JLS defines the language, therefore
there is no higher authority.

Mike Schilling wrote:
> "Operator" means different things to different people.  A parser guy
> would probably call "new" an operator; because it combines expressions
> into a more complex expression and has a defined precedence. But the
> "offical" list of operators is found at
> <http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.12>
>

This link has been posted before in this thread. OP: you should read
it.

> and doesn't include cast, new, or parentheses.
>

nor "dot". However, the JLS does refer to 'instanceof' as an
operator.

This question has now been answered several times in this thread. You
might want to consider accepting the answer.

Robbo wrote:
>> And the second thing. In your table there is (cast) operator
>> at precedence of 1 (it has the same priority as prefix and unary
>> operators). Also association is "Right".

Don't you mean "associativity"?

>> If I am right (byte)++x means that ++ is computed before (byte)

>> -- both (cast) and prefix ++ have the same priority and association [sic]


>> is "Right".
>> And in (byte)x++,  (byte) is first because of higher priority of
>> cast than postfix ++.
>> What do you think?
>

Mike Schilling wrote:
> Other way around.  Postfix ++ has a higher precedence than the cast
> does.

I believe the issue here is one of associativity, not precedence.
Both cast and postfix operators are right-associative, therefore the
postfix operator is evaluated first.

The precedence is indicated by the order in which the operators and
other expressions (e.g., cast expressions) appear in chapter 15 of the
JLS.
<http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html>

and is defined by the productions in
<http://java.sun.com/docs/books/jls/third_edition/html/
syntax.html#18.1>

--
Lew

Lew

unread,
Jan 25, 2010, 5:13:59 PM1/25/10
to
Robbo wrote:
> It means there is mistake in Mr. Doedy Green's table
> (http://mindprod.com/jgloss/precedence.html).
> In this table prefix and postfix has the same priority -- probably
> simple mistake with numbers of precedence.
> But more serious mistake in his table is that prefix is before
> postfix. According to
> <http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html>
> postfix goes first.
>

Roedy has prefix/unary at the same level of precedence as postfix, not
at a higher level. Still a difference, but one that makes no
difference since right-associativity would still have postfix
operators evaluated first.

> I really wish official table of Java operators with precedence
> and associations.

You just cited an official table. Didn't that grant your wish?
<<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/
operators.html>
(copied and pasted from your post)

Why are you asking for something you already have?

> In C++ situations is not better. E.g. [sic] here


> <http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B>
> there is information that postfix has precedence 2 and prefix has
> precedence 3. In my Polish edition of Stroustrup's book
> postfix and prefix has the same precedence.
>

That's off topic, but let me suggest that those differences may be due
to changes in the language, perhaps. Not that I really know.

--
Lew

Robbo

unread,
Jan 25, 2010, 5:20:23 PM1/25/10
to
After reading a few resources, I created such table:

Level Category Operator Associativity
---------------------------------------------------------------
1 postfix expr++ expr-- left
---------------------------------------------------------------
2 prefix ++expr --expr right
unary +expr -expr right
logical NOT ! right

bitwise NOT ~ left
---------------------------------------------------------------
3 cast (type) right
---------------------------------------------------------------
4 multiplicative * / % left
---------------------------------------------------------------
5 additive + - left
---------------------------------------------------------------
6 shift << >> >>> left
---------------------------------------------------------------
7 relational < <= > >= left
type comparison instanceof
---------------------------------------------------------------
8 equality == != left
---------------------------------------------------------------
9 bitwise AND & left
---------------------------------------------------------------
10 bitwise XOR ^ left
---------------------------------------------------------------
11 bitwise OR | left
---------------------------------------------------------------
12 logical AND && left
---------------------------------------------------------------
13 logical OR || left
---------------------------------------------------------------
14 conditional ?: right
---------------------------------------------------------------
15 assignment = += -= *= /= right
%= &= ^= |=
<<= >>= >>>=
---------------------------------------------------------------

What do you think about this?
It contains only real Java operators, I hope.

I have a few problems with this table:
1. According to some resources, postfix and prefix
have the same level (precedence). It is right?
2. How about associativity of ~ ?
3. How about level (precedence) of "cast"?
Shouldn't it be in level 2?

Mike Schilling

unread,
Jan 25, 2010, 5:20:43 PM1/25/10
to
Lew wrote:
> Mike Schilling wrote:
>> Other way around. Postfix ++ has a higher precedence than the cast
>> does.
>
> I believe the issue here is one of associativity, not precedence.

No, it really is precedence. You can't sensibly define associatively
among a group of unary operators that include both left-hand and
right-hand operators. That's why the two postfix operators (++ and --)
are at a precedence level all their own, while the corresponding
prefix operators share a level with casts.

> Both cast and postfix operators are right-associative, therefore the
> postfix operator is evaluated first.

Postfix operators aren't associative at all, since you can't use two
of them, e.g.. i++++ is iilegal.


znôrt

unread,
Jan 25, 2010, 5:27:21 PM1/25/10
to
On Mon, 25 Jan 2010 23:20:23 +0100, Robbo <nie...@yle.com> wrote:

> What do you think about this?

that you ...

a). are working on yet another compiler/parser/whatever or
b). have way too much time and too little to do :-)

bytheway, parenthesis do a great job making abritrarily complex
expressions evident to anyone reading any code. they have the (great)
added benefit of being language independent and widely recognised. just
use them! (if you really are in need for such complex expressions).

Mike Schilling

unread,
Jan 25, 2010, 5:29:30 PM1/25/10
to
Lew wrote:

> However, the JLS does refer to 'instanceof' as an
> operator.

But it's not in the list of operators, so the JLS contradicts itself.
It really is the Java programmer's Bible!

More seriously, the official JLS definition of "operator" describes
only operators made up of special characters; that is, it
distinguishes "operator" from "keyword". If "new" were spelled "^" as
in

InputStream strm = ^FileInputStream(filename);

the JLS would presumably call it an operator. If you think of
operator as a semantic term rather than a syntactical one, you're
likely to define it to include "instanceof" and "new".


Robbo

unread,
Jan 25, 2010, 5:46:59 PM1/25/10
to
I am just a man who like to know ;)
I will not rest, if I do not solve this problem first.

Robbo


znôrt

unread,
Jan 25, 2010, 5:58:53 PM1/25/10
to
On Mon, 25 Jan 2010 23:46:59 +0100, Robbo <nie...@yle.com> wrote:
> I am just a man who like to know ;)
> I will not rest, if I do not solve this problem first.

well, good so. :-)

I guess then your only path is chewing through the JLS, because no other
information outside of that context will make sense.

Eventually, you will stumble across:

<jls>
15.7 Evaluation Order
...
It is recommended that code not rely crucially on this specification. Code
is usually clearer when each expression contains at most one side effect,
as its outermost operation, and when code does not depend on exactly which
exception arises as a consequence of the left-to-right evaluation of
expressions.
</jls>
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.7

which is part of the specification and makes a lot of sense to me, and
makes me a happy man knowing just that :D

Robbo

unread,
Jan 25, 2010, 7:10:25 PM1/25/10
to
> I guess then your only path is chewing through the JLS, because no other
> information outside of that context will make sense.

I fixed "associativity" in my table, according to JLS.
When there is "+" symbol, it means that it is confirmed
according to JLS.

If you ask, how about levels (precedence)? I have not made any progress.
There is no exact information in JLS about precedences
of many operators. Probably only way is to read grammar :(
But on the other hand, I am not sure if grammar included such
information as procedence of operators.

Level Category Operator Associativity
---------------------------------------------------------------
1 postfix expr++ expr-- left
---------------------------------------------------------------

2 prefix ++expr --expr right+
unary +expr -expr
logical NOT !
bitwise NOT ~
---------------------------------------------------------------
3 cast (type) right+
---------------------------------------------------------------
4 multiplicative * / % left+
---------------------------------------------------------------
5 additive + - left+
---------------------------------------------------------------
6 shift << >> >>> left+
---------------------------------------------------------------
7 relational < <= > >= left+
type comparison instanceof
---------------------------------------------------------------
8 equality == != left+
---------------------------------------------------------------
9 bitwise AND & left+
---------------------------------------------------------------
10 bitwise XOR ^ left+
---------------------------------------------------------------
11 bitwise OR | left+
---------------------------------------------------------------
12 logical AND && left+
---------------------------------------------------------------
13 logical OR || left+
---------------------------------------------------------------
14 conditional ?: right+
---------------------------------------------------------------
15 assignment = += -= *= /= right+

Joshua Cranmer

unread,
Jan 25, 2010, 8:07:32 PM1/25/10
to
On 01/25/2010 07:10 PM, Robbo wrote:
> ---------------------------------------------------------------
> 2 prefix ++expr --expr right+
> unary +expr -expr
> logical NOT !
> bitwise NOT ~
> ---------------------------------------------------------------
> 3 cast (type) right+

Actually, UnaryExpressionNotPlusMinus contains CastExpression, so it is
actually at the same precedence level as logical and bitwise NOT, which
is one higher than the prefix/unary +/unary - (UnaryExpression contains
PreIncrementExpression, PreDecrementExpression, and the unary +/unary -).

But unary expression precedence is kind of a moot point.

Lew

unread,
Jan 25, 2010, 9:07:13 PM1/25/10
to
Mike Schilling wrote:
> Lew wrote:
>
>> However, the JLS does refer to 'instanceof' as an
>> operator.
>
> But it's not in the list of operators, so the JLS contradicts itself.
> It really is the Java programmer's Bible!
>
> More seriously, the official JLS definition of "operator" describes
> only operators made up of special characters; that is, it
> distinguishes "operator" from "keyword".

I was just going by
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2>
entitled "Type Comparison Operator instanceof". That looks an awful lot like
an "official JLS definition" to me.

--
Lew

Lew

unread,
Jan 25, 2010, 9:11:07 PM1/25/10
to
Robbo wrote:
> Level Category Operator Associativity
> ---------------------------------------------------------------
> 1 postfix expr++ expr-- left

As I was corrected upthread (perhaps you missed that post), there is no
associativity for postfix operators.

--
Lew

Mike Schilling

unread,
Jan 25, 2010, 10:13:24 PM1/25/10
to

Sorry, I meant that 15.20.2 disagrees with the list in 3.12. Which is
fine; it's not as if giving a precise defintions of "operator" is one
of the JLS's most important goals.

This reminds me of a discussion that went on in comp.lang.c years ago.
Someone was asking whether "3 + 12i" was an imaginary number or not.
He'd seen some references that said "3 + 12i" was imaginary but not
"pure imaginary", and others that said "3 + 12i" was not imaginary
because it had a real part. He wanted to know the answer, because,
dammit, in mathematics there's always one right answer. No one could
convince him otherwise.


Patricia Shanahan

unread,
Jan 25, 2010, 10:31:46 PM1/25/10
to
Robbo wrote:
...

> 1. According to some resources, postfix and prefix
> have the same level (precedence). It is right?
...

++(a--) and (++a)-- both apply an operator requiring a variable to a
non-variable value. ++a-- is an error.

Patricia

Lew

unread,
Jan 25, 2010, 10:37:02 PM1/25/10
to
Mike Schilling wrote:
> This reminds me of a discussion that went on in comp.lang.c years ago.
> Someone was asking whether "3 + 12i" was an imaginary number or not.
> He'd seen some references that said "3 + 12i" was imaginary but not
> "pure imaginary", and others that said "3 + 12i" was not imaginary
> because it had a real part. He wanted to know the answer, because,
> dammit, in mathematics there's always one right answer. No one could
> convince him otherwise.

That was a much more complex question, though.

--
Lew

Robbo

unread,
Jan 26, 2010, 6:30:55 AM1/26/10
to
> Actually, UnaryExpressionNotPlusMinus contains CastExpression, so it is
> actually at the same precedence level as logical and bitwise NOT, which
> is one higher than the prefix/unary +/unary - (UnaryExpression contains
> PreIncrementExpression, PreDecrementExpression, and the unary +/unary -).


UnaryExpressionNotPlusMinus contains PostFixExpression also.

To be honest, I do not understand Your sentence above.
Does it mean that "cast" is at the same level as ~ and ! but
there should be one more level (above ~ ! and cast)
with unary +/-/--expr/++expr?

1. expr++, expr--
2. ++expr, --expr, +expr, -expr
3. ~ ! (type)


Robbo

unread,
Jan 26, 2010, 6:31:57 AM1/26/10
to
> As I was corrected upthread (perhaps you missed that post), there is no
> associativity for postfix operators.

Ok. Thx.


Level Category Operator Associativity
---------------------------------------------------------------

1 postfix expr++ expr-- +

Roedy Green

unread,
Jan 26, 2010, 7:01:24 AM1/26/10
to
On Mon, 25 Jan 2010 21:31:31 +0100, "Robbo" <nie...@yle.com> wrote,

quoted or indirectly quoted someone who said :

>Where did you find information that "new" is officially


>operator in Java? I am really interested in this topic.
>Patricia Shanahan (in this thread) has said that "new" is not an operator.
>I have not found any information that would confirm
>that "new" is an operator in JLS (3rd edition).

I think I read it in Java in a Nutshell, back in the Java 1.0 days.

The JLS has refined over the years. Whatever it has to say now about
new is the official story.

Roedy Green

unread,
Jan 26, 2010, 7:10:09 AM1/26/10
to
On Mon, 25 Jan 2010 21:31:31 +0100, "Robbo" <nie...@yle.com> wrote,

quoted or indirectly quoted someone who said :

>If I am right (byte)++x means that ++ is computed before (byte)


>-- both (cast) and prefix ++ have the same priority and association
>is "Right".
>And in (byte)x++, (byte) is first because of higher priority of cast
>than postfix ++.
>What do you think?

My rule of thumb is, if I have to think about precedence, it won't be
clear to many other people reading either, and I had better use some
more parentheses.

There is a fun tool in intelliJ. You write your expression larded
with (), and then you ask it to remove nugatory ones. After viewing a
number of examples you get to recognise common patters and gotchas.

You can also perform some experiments to see how things evaluate.

You need to get precedence on a visual level in your brain so you just
KNOW how an expression will evaluate. Only at the beginning to you
look and precedence numbers and associativity. You learn common
patterns, and put it on automatic.

I have a mental model that operators are like little gremlins with two
little arms that reach out to operands. Some have weak arms. Some
have strong ones. When they fight it out, a natural grouping forms,
where the strong ones hold the operands more tightly to them.

Perhaps someone might create an expression displayer than uses white
space around operators to visually and subliminally display the
implied precedence.

In JDisplay I used various sizes of ( ) and { }. Perhaps something
could be done along those lines to help you proofread code.

Lew

unread,
Jan 26, 2010, 8:32:01 AM1/26/10
to
Roedy Green wrote:
> Perhaps someone might create an expression displayer than uses white
> space around operators to visually and subliminally display the
> implied precedence.

I have one of those.

--
Lew

Lew

unread,
Jan 26, 2010, 8:39:47 AM1/26/10
to
Please attribute quotations.

Lew wrote:
>> As I was corrected upthread (perhaps you missed that post), there is no
>> associativity for postfix operators.

Robbo wrote:
> Ok. Thx.
>
>
> Level Category Operator Associativity
> ---------------------------------------------------------------
> 1 postfix expr++ expr-- +
> ---------------------------------------------------------------
> 2 prefix ++expr --expr right+

The same reasoning applies to prefix auto{in,de}crement.

It's been pointed out before, although to be fair, to reach the conclusion it
would have required thinking about what Patricia posted.


> ++(a--) and (++a)-- both apply an operator
> requiring a variable to a non-variable value.

There's no point talking about associativity of prefix ++ if ++++x is an
illegal expression. (Or if it means that unary + is applied twice to ++x - I
didn't try it. Why don't you? It'll be a fun experiment.)

Thinking about things is a fundamental tool in the programmer's toolbox.

--
Lew

Thomas Pornin

unread,
Jan 26, 2010, 9:54:15 AM1/26/10
to
According to Lew <no...@lewscanon.com>:

> There's no point talking about associativity of prefix ++ if ++++x is an
> illegal expression.

There is no "associativity" at all for unary operators. Associativity is
a notion which is defined only for binary operators.

A unary prefix operator applies to what is on its right. A unary postfix
operator applies to what is on its left. There is no ambiguity to be
lifted with an associativity rule. For instance, "- -x" is to be parsed
as "-(-x)" and nothing else, because the unary minus is a prefix
operator, not because of some supposed associativity.


To be fair, there are some textbooks which talk about "associativity of
unary operators" as a convoluted way to say that postfix operators
actually have higher or lower precedence than prefix operators. In such
texts, you would see that "unary ++ and -- are right-associative" which,
in that context, means "postfix ++ and -- have slightly higher
precedence than prefix ++ and --". If, in a table of operators, you do
not list some prefix and postfix operators as having the same precedence
(e.g. you isolate postfix ++ and -- on a line of their own), then you
have no need for such contriving of the notion of associativity.


> (Or if it means that unary + is applied twice to ++x - I
> didn't try it. Why don't you? It'll be a fun experiment.)

Java lexing is "greedy" (like in C or C++). This is covered in the
JLS, section 3.2:

The longest possible translation is used at each step, even if the
result does not ultimately make a correct program while another
lexical translation would. Thus the input characters a--b are
tokenized as a, --, b, which is not part of any grammatically correct
program, even though the tokenization a, -, -, b could be part of a
grammatically correct program.

Doing otherwise would make the parser code quite more complex.


--Thomas Pornin

Eric Sosman

unread,
Jan 26, 2010, 11:28:34 AM1/26/10
to
On 1/25/2010 3:31 PM, Robbo wrote:
> [...]

> And in (byte)x++, (byte) is first because of higher priority of cast
> than postfix ++.
> What do you think?

Even without checking the JLS, it's easy to see that
this is wrong. If the cast had higher precedence, the
expression would be equivalent to

( (byte)x )++

... which is nonsense. Consider a wider context like

double x = 42.0;
( (byte)x )++;

... and ponder: "What primitive does the ++ operator modify?"

Therefore: If the cast has higher precedence the compiler
must reject the expression, so if the compiler accepts it the
cast does not have higher precedence.

--
Eric Sosman
eso...@ieee-dot-org.invalid

Tom Anderson

unread,
Jan 26, 2010, 1:17:02 PM1/26/10
to
On Tue, 26 Jan 2010, Thomas Pornin wrote:

> According to Lew <no...@lewscanon.com>:
>> There's no point talking about associativity of prefix ++ if ++++x is an
>> illegal expression.
>
> There is no "associativity" at all for unary operators. Associativity is
> a notion which is defined only for binary operators.

True. The equivalent for unary operators is probably distributivity, or
whatever it's called. Like how -(a+b) == (-a)+(-b). Although of course
-(a*b) != (-a)*(-b), so it's a bit more complicated. And not at all
related to drawing up tables of operators.

tom

--
hypnopomp rapist

Robbo

unread,
Jan 26, 2010, 4:39:50 PM1/26/10
to

U�ytkownik "Thomas Pornin" <por...@bolet.org> napisa� w wiadomo�ci
news:4b5f0217$0$29839$426a...@news.free.fr...

> According to Lew <no...@lewscanon.com>:
> > There's no point talking about associativity of prefix ++ if ++++x is an
> > illegal expression.
>
> There is no "associativity" at all for unary operators. Associativity is
> a notion which is defined only for binary operators.


JLS 3d edition, point 15.15 Unary operators:

"The unary operators include +, -, ++, --, ~, !, and cast operators.
Expressions with unary operators group right-to-left, so that -~x
means the same as -(~x)."

Message has been deleted
0 new messages