It's not a secret anymore!
The moose likes Java in General and the fly likes + - operation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "+ - operation" Watch "+ - operation" New topic
Author

+ - operation

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
Try the following code:

The "+ -" operation does not produce a compile time error. Effectively it, negates the second operand, to produce x - y. This makes some sense because we often say . Still, I can't find this explicitly stated in the spec. Does anyone know where this behavior is given (either directly or more likely indirectly)?
(We came across this when a developer made a typo and got the wrong numerical result. We were surprised, at first, because we would have figured it would cause a compile time error.)

--Mark
hershey@vaultus.com
Hassan Naqvi
Ranch Hand

Joined: May 03, 2001
Posts: 158
Hi,
z=x + - y;
Since the precedence of + & - are same therefore compiler will check their associativity which is left to right & take - as a unary operator.You can say it,
z = x + (- y);
Regards,
Hassan.


Always Belive On Logic!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
As Hassan says, the - is a unary operator. However the JLS doesn't say anything about associativity - instead it laboriously defines the grammar of Java using grammar notation, spread throughout the spec. Concepts like operator precedence and associativity are not explicitly defined the the spec; rather, they are implicit in the grammatical definitions found in the spec.
To parse the statement
<code><pre> z = x + - y;</pre></code>
we look first to JSL 14.8:
<pre>
ExpressionStatement:
StatementExpression ;
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
</pre>
This means that an ExpressionStatement is defined as a StatementExpression followed by a semicolon, and a StatementExpression may consist of a single Assignment (among other possibilities).
Now from section JLS 15.26:
<pre>
Assignment:
LeftHandSide AssignmentOperator AssignmentExpression
LeftHandSide:
ExpressionName
FieldAccess
ArrayAccess
AssignmentOperator: one of
= *= /= %= += -= <<= >>= >>>= &= ^= |=
</pre>
The LeftHandSide is the ExpressionName "z". The AssimentOperator is "=". And the AssignmentExpression is "x + - y". Specifically, it must be a ConditionalExpression, since we've already parsed the "=" which made the overall expression an Assignment. Think of the name as meaning "an expression which might have a conditional" rather than "an expression which does have a conditional" and it will make a little more sense - the names can be misleading.
From JLS 15.25:
<pre>
ConditionalExpression:
ConditionalOrExpression
ConditionalOrExpression ? Expression : ConditionalExpression
</pre>
We don't have a '?' or ':', so "x + - y" must be a ConditionalOrExpression. We will descend through multiple expression types from here, in order (JLS sections 15.24 - 15.19):
<pre>
ConditionalOrExpression
ConditionalAndExpression
InclusiveOrExpression
ExclusiveOrExpression
AndExpression
EqualityExpression
RelationalExpression
ShiftExpression
</pre>
This eventually leads to AdditiveExpression in JLS 15.18:
<pre>
AdditiveExpression:
MultiplicativeExpression
AdditiveExpression + MultiplicativeExpression
AdditiveExpression - MultiplicativeExpression
</pre>
Suddenly, we have two possible matches:
AdditiveExpression (x) + MultiplicativeExpression (- y)
or
AdditiveExpression (x -) + MultiplicativeExpression (y)
However, if you continue to descend down the definitions, there will never be a way of parsing "x -" as an AdditiveExpression. There is however a way to parse "- y" as a MultiplicativeExpression in JLS 15.17:
<pre>
MultiplicativeExpression:
UnaryExpression
MultiplicativeExpression * UnaryExpression
MultiplicativeExpression / UnaryExpression
MultiplicativeExpression % UnaryExpression
</pre>
And JLS 15.15:
<pre>
UnaryExpression:
PreIncrementExpression
PreDecrementExpression
+ UnaryExpression
- UnaryExpression
UnaryExpressionNotPlusMinus
</pre>
You can further trace the definitions of "x" and "y" to:
<pre>UnaryExpressionNotPlusMinus
PostfixExpression
ExpressionName
</pre>
I know this is tedious, and I omitted a number of steps, but hopefully it gives you an understanding of how the official grammar works. Enjoy.

[This message has been edited by Jim Yingst (edited May 07, 2001).]


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: + - operation
 
Similar Threads
Convert 1d array to 2D boolean
Can't a switch use final parameters?
Case constant must be compile time constant?
casting doubt
question about variables changed inside methods