41

According to this table of Java operator precedence and associativity, member access has higher precedence than the new operator.

However, given a class myClass and a non-static member function myFunction, the following line of code is valid:

new myClass().myFunction();

If . is evaluated before new, how can this line be executed? In other words, why aren't parentheses required?

(new myClass()).myFunction();

My guess is that since () shares precedence with ., the myClass() is evaluated first, and so the compiler knows even before evaluating the new keyword that the myClass constructor with zero parameters is being called. However, this still seems to imply that the first line should be identical to new (myClass().myFunction());, which is not the case.

17
  • 8
    That table is not a normative reference. You can't cite it as evidence. Commented May 13, 2013 at 0:20
  • 3
    Don't put words into my mouth. You know what I said. You can't cite a third party website as evidence for the operator precedence in Java. You can't cite an SO answer either unless it cites or quotes from a normative reference. In this case it is obvious that it is the 3rd party site that is wrong. I don't know why we're even discussing it. Commented May 13, 2013 at 1:36
  • 1
    @EJP Okay, I misunderstood. I'm only "putting words in your mouth" because I'm trying to better understand what you're saying (and at this point why you're being such a jerk about it). The statement "you can't cite it as evidence" seems to imply that it's not valid as a reference source. So are you saying that it's fine to use non-normative references, but not okay to reference them in SO questions and answers? Commented May 13, 2013 at 1:42
  • 1
    'Non-normative' means 'not valid as a reference source'. I have made no statement about using such sources in your daily work one way or the other, and I will not have words put into my mouth about it. I will add that in several decades on the Internet and its predecessors, I have observed that whenever a question starts 'so you are saying' the answer is invariably 'no'. Commented May 13, 2013 at 2:16
  • 2
    @EJP Weirdly enough, I reread this discussion every so often, and every time it boggles my mind. I'm not sure why we had such a hard time communicating, but I wonder if it's largely because I really don't know what you meant by "cite it as evidence." This isn't a court of law; I wasn't trying to prove that Java behaves (or should behave) in a certain way. I was simply making a genuine effort to understand a part of the language that I found confusing, and I used the link to explain why I was confused. Commented Mar 8, 2015 at 10:02

2 Answers 2

47

This is because of how the grammar of Java language is defined. Precedence of operators comes into play just when the same lexical sequence could be parsed in two different ways but this is not the case.

Why?

Because the allocation is defined in:

Primary: ... new Creator 

while method call is defined in:

Selector: . Identifier [Arguments] ... 

and both are used here:

Expression3: ... Primary { Selector } { PostfixOp } 

so what happens is that

new myClass().myFunction(); 

is parsed as

 Expression | | ---------+-------- | | | | Primary Selector | | | | ---+--- ... | | new Creator 

So there is no choice according to priority because the Primary is reduced before. Mind that for the special situation like

new OuterClass.InnerClass() 

the class name is actually parsed before the new operator and there are rules to handle that case indeed. Check the grammar if you like to see them.

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't it should be: new OuterClass.NestedClass() in the last example? I think you intended to mention 'static' nested class.
3

I disagree with the conclusion drawn from Jack's diagram. When a grammar is written its nonterminals and structure are designed to implement the precedence and associativity of the language being described. That's why the classic BNF for expressions introduces the "term" and "factor" nonterminals - to enforce the normal multiplication before addition precedence of arithmetic.

So the fact that "Primary -> new Creator" and "Expression -> Primary Selector" in the grammar means that "new Creator" is at a higher precedence level than "Primary Selector".

It seems to me that the grammar is evidence that the Java operator precedence and associativity table is incorrect.

3 Comments

It seems the specific premise in Jack's answer with which you disagree is "Precedence of operators comes into play just when the same lexical sequence could be parsed in two different ways". And indeed some grammars may be ambiguous as to order of operations, and may have a precedence table in addition to the grammar specification itself.
But it appears that you are correct that this is not the case for Java, because Chapter 15 of the spec says: "Precedence among operators is managed by a hierarchy of grammar productions. The lowest precedence operator is the arrow of a lambda expression (->), followed by the assignment operators." Your answer would be improved by citing this quote as evidence.
(I realize this quote is from a newer revision of the spec, since when I asked this question lambdas hadn't been introduced in Java yet.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.