Re: Looking for good table with all Java operators

Discussion in 'Java' started by John B. Matthews, Jan 25, 2010.

  1. In article <hjioae$jef$>,
    "Robbo" <> 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>
     
    John B. Matthews, Jan 25, 2010
    #1
    1. Advertising

  2. John B. Matthews

    Lew Guest

    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"?

    John B. Matthews wrote:
    > 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>


    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
     
    Lew, Jan 25, 2010
    #2
    1. Advertising

  3. John B. Matthews

    Lew Guest

    Robbo wrote:
    >> Those aren't operators.


    Please attribute citations.

    > If not, why tables of operators you provided me with include () [] and . ?
    >
    > Look:
    >
    >>> Princeton:
    >>> <http://www.cs.princeton.edu/introcs/11precedence/>
    >>>
    >>> University of West Florida:
    >>> <http://www.cs.uwf.edu/~eelsheik/cop2253/resources/op_precedence.html>


    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
     
    Lew, Jan 25, 2010
    #3
  4. John B. Matthews

    Roedy Green Guest

    On Mon, 25 Jan 2010 13:22:09 +0100, "Robbo" <> 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.

    --
    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, Jan 25, 2010
    #4
  5. On 01/25/2010 10:46 AM, Robbo wrote:
    > 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".


    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
     
    Joshua Cranmer, Jan 25, 2010
    #5
  6. John B. Matthews

    Roedy Green Guest

    On Mon, 25 Jan 2010 14:00:36 +0100, "Robbo" <> 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.

    --
    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, Jan 25, 2010
    #6
  7. 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.
     
    Mike Schilling, Jan 25, 2010
    #7
  8. John B. Matthews

    Lew Guest

    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, Jan 25, 2010
    #8
  9. John B. Matthews

    Lew Guest

    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
     
    Lew, Jan 25, 2010
    #9
  10. 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.
     
    Mike Schilling, Jan 25, 2010
    #10
  11. 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".
     
    Mike Schilling, Jan 25, 2010
    #11
  12. John B. Matthews

    Lew Guest

    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, Jan 26, 2010
    #12
  13. Lew wrote:
    > 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.


    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.
     
    Mike Schilling, Jan 26, 2010
    #13
  14. John B. Matthews

    Lew Guest

    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
     
    Lew, Jan 26, 2010
    #14
  15. John B. Matthews

    Roedy Green Guest

    On Mon, 25 Jan 2010 21:31:31 +0100, "Robbo" <> 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 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, Jan 26, 2010
    #15
  16. John B. Matthews

    Roedy Green Guest

    On Mon, 25 Jan 2010 21:31:31 +0100, "Robbo" <> 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.





    --
    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, Jan 26, 2010
    #16
  17. John B. Matthews

    Lew Guest

    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, Jan 26, 2010
    #17
  18. John B. Matthews

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Jan 26, 2010
    #18
    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. Frank Ratzlow
    Replies:
    3
    Views:
    1,690
    Frank Ratzlow
    Aug 1, 2003
  2. JPR105
    Replies:
    0
    Views:
    395
    JPR105
    Jan 24, 2004
  3. Yoav
    Replies:
    2
    Views:
    381
    Roedy Green
    Apr 25, 2004
  4. mike3
    Replies:
    5
    Views:
    319
    James Kanze
    May 27, 2007
  5. Lew
    Replies:
    2
    Views:
    306
    Tom Anderson
    Jan 26, 2010
Loading...

Share This Page