问题
I had an expression with increment and decrement operators along with some binary operators.
public class precedence {
public static void main(String ar[]){
int a=3;
int b=4;
System.out.println(a++ * b-- / a-- + ++b);
System.out.println(a+","+b);
}
}
first ++b is replaced by 5 and b will be 5.
Then as all remaining terms are post fix versions the order of evaluation will be from right to left
a-- will be replaced with 3 and a is changed as 2.
b-- will be replaced with 5 and b becomes 4.
a++ will be replaced with 2 and a becomes 3.
So the final expression should be 2 * 5 / 3 + 5 which is equal to 8 but the answer shown in the output is 7. Can some one tell me where I am wrong.
回答1:
For a start, if you have something like:
A * B / C + D
the expression is evaluated left to right, because there's no precedence, parentheses or associativity overriding that order, as per the Java Language Spec, section 15.7, Evaluation order
:
The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.
JLS section 15.7.3
can override this normal order:
The Java programming language respects the order of evaluation indicated explicitly by parentheses and implicitly by operator precedence.
But that's not the case here since division and multiplication have higher precedence than addition, and there are no parentheses involved.
In addition, 15.7.1 states clearly (my emphasis):
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Hence the side effects belong with the sub-expression evaluation, not the entire expression. That means, in something like a * --a
, it is impossible for the decrement of a
to affect the left hand side of the multiplication, which has already been fully evaluated.
Hence, in the expression shown above, A * B / C + D
, D
will actually be evaluated last and any side-effect attached to it won't happen before everything else is already evaluated. In addition, any side effects arising from the evaluation of A
, B
or C
will have occurred before D
is actioned.
Applying that reasoning to your side effect expression, which is effectively:
(a++) * (b--) / (a--) + (++b)
- initially,
a
is3
,b
is4
. a++
uses3
, setsa
to4
.b--
uses4
, setsb
to3
.a--
uses4
, setsa
to3
.++b
setsb
to4
, uses4
.
So:
3 * 4 / 4 + 4
= 12 / 4 + 4
= 3 + 4
= 7
回答2:
Lets take the concerned statement
int a=3;
int b=4;
System.out.println(a++ * b-- / a-- + ++b);
Step 1: (
a++
* b-- / a-- + ++b)
to (
3
* b-- / a-- + ++b)
a=4,b=4
Step 2: (a++ *
b--
/ a-- + ++b)
to (3 *
4
/ a-- + ++b)
a=4,b=3
Step 3: (a++ * b-- /
a--
+ ++b)
to (3 * 4 /
4
+ ++b)
a=3,b=3
Step 4: (a++ * b-- / a-- +
++b
)
to (3 * 4 / 4 +
4
)
a=3,b=4
So the final expression is (3 * 4 / 4 + 4)
Hope this clarifies:)
回答3:
int a=3;
int b=4;
System.out.println(a++ * b-- / a-- + ++b);
System.out.println(a+","+b);
You are using following operators in you statement. The operator are listed into their precedence order.
- post-increment, post-decrement
- pre-increment
- multiplicative (*, /, +, -)
So a++
(3, a=4)
, b--
(4, b=3)
and a--
(4, a=3)
will be executed in the order from left to right. Then ++b
(4, b=4)
will be executed. Now the multiplicative operators will be executed in left to right order. So (3 * 4 / 4 + 4).
(3 * 4 / 4 + 4) ==> (12 / 4 + 4) ==> (3 + 4) ==> 7
So the output is : 7.
Final values of a=3 and b=4.
Edit: Ask in the comment.
a=3,b=4;
expression : ++b * --a + ++b;
The ++b
(5)
, --a
(2)
, ++b
(6)
will be executed in left to right order.
Now the expression is (5 * 2 + 6)
==> (10 + 6)
==> (16)
.
You can check this post related to this. I have referenced it. Link.
回答4:
System.out.println(a++ * b-- / a-- + ++b);
System.out.println(3 * 4 / 4 + 4);
You have to read it like this:
Whenever you see the variable, it gets returned.
So if you have a++
, it will return it first and then increment it.
If you have ++a
, it will increment it first and then return it.
回答5:
Hi it follows "bodmas"
B Brackets first
O Orders (ie Powers and Square Roots, etc.)
DM Division and Multiplication (left-to-right)
AS Addition and Subtraction (left-to-right)
int a=3;
int b=4;
System.out.println(a++ * b-- / a-- + ++b);
a++ * b-- / a-- + ++b
3*(4/4) +4
3*1 +4
3 +4
回答6:
a++ * b-- / a-- + ++b = 3*4/4+4
Now apply DMAS rule, so 3*1+4 = 3+4 = 7
来源:https://stackoverflow.com/questions/32578869/confusion-using-increment-and-decrement-operators-in-java