Reputation: 563
First, let's take a look at a very simple piece of code.
class S { int x = 0; }
class Test1 {
public static void main(String[] args) {
S s = new S();
System.out.println("s.x=" + s.x);
}
}
s.x
is a field access expression.
The following is the relevant syntax excerpted by me from the Java Language Specification:
15.11:
FieldAccess:
Primary . Identifier
super . Identifier
TypeName . super. Identifier
15.8: Primary:
PrimaryNoNewArray
ArrayCreationExpression
PrimaryNoNewArray:
Literal
ClassLiteral
this TypeName . this
( Expression )
ClassInstanceCreationExpression
FieldAccess
ArrayAccess
MethodInvocation
MethodReference
If only looking at the syntax excerpted above, then s.x
seemingly does not conform to the syntax.The reason is that s
seemingly does not belong to any member of the Primary expressions.
Fortunately, the specification gives an explanation later, and this is exactly where my doubt lies:
This part of the grammar of the Java programming language is unusual, in two ways. First, one might expect simple names, such as names of local variables and method parameters, to be primary expressions. For technical reasons, names are grouped together with primary expressions a little later when postfix expressions are introduced (§15.14).
"names are grouped together with primary expressions a little later when postfix expressions are introduced",My understanding of this sentence is that a name will become a member of the primary expressions under certain circumstances.
15.14:
Postfix expressions include uses of the postfix ++ and -- operators. Names are not considered to be primary expressions (§15.8), but are handled separately in the grammar to avoid certain ambiguities. They become interchangeable only here, at the level of precedence of postfix expressions.
My question is: 1.Is my above understanding correct?
2."They become interchangeable only here, at the level of precedence of
postfix expressions",
(1).What does this sentence mean?
(2).Can you explain, based on my example, why s
in s.x
becomes a member of the primary expressions here?(On the premise that my understanding is correct.)
(3).What is the relationship between this and the level of precedence of postfix expressions?
(4).What does "interchangeable" refer to?
Thank you for your reading.
Upvotes: -1
Views: 111
Reputation: 281757
s.x
is not a primary expression, and does not "become a member of the primary expressions" at any point. s.x
is parsed as an ExpressionName:
ExpressionName:
Identifier
AmbiguousName . Identifier
AmbiguousName:
Identifier
AmbiguousName . Identifier
which is one of the options for a PostfixExpression, alongside Primary:
PostfixExpression:
Primary
ExpressionName
PostIncrementExpression
PostDecrementExpression
When the JLS says that names and primary expressions "become interchangeable" at the level of precedence of postfix expressions, it means that both names and primary expressions can be parsed as postfix expressions. It does not mean that names become primary expressions.
Upvotes: 4