I read from the official tutorial of Java that prefix and postfix ++ -- have different precedences:
postfix: expr++ expr--
unary: ++expr --expr +expr -expr ~ !
Operators
According to the tutorial, shouldn't this
d = 1; System.out.println(d++ + ++d);
print out 6 (d++
makes d 2, ++d
makes it 3) instead of 4?
I know the explanation of ++d
being evaluated beforehand, but if d++
has higher precedence then ++d
, why isn't d++
being first evaluated? And what is more, in what case should d++
shows that it has higher precedence?
EDIT:
I tried the following:
d = 1; System.out.println(++d * d++);
It returns 4. It开发者_如何学运维 seems that it should be 2*2, instead of 1*3.
The inside of the println statement is this operation (d++) + (++d)
- It is as follows, the value of d is read (d = 1)
- current value of d (1) is put into the addition function
value of d is incremented (d = 2).
Then, on the right side, the value of d is read (2)
- The value of d is incremented (now d = 3)
Finally, the value of d (3) is put into the addition function
thus 1 + 3 results in the 4
edit: sorry for the format, I'm rather bad at using the list haha
The key is what is returned from the operation.
- x++ changes the value of x, but returns the old x.
- ++x changes the value of x, and returns the new value.
d=1 System.out.println(d++ + ++d); // d is 1 System.out.println(1 + ++d); // d is 2 System.out.println(1 + 3); // d is 3
Prints 4
Different precedence does not mean will be evaluated first.
It means the expressions will be grouped in this way.
In this case, d++ + ++d
will be grouped (d++) + (++d)
, and this binary expression will be evaluated in this order:
- left operand
d++
. This subexpression consists of a postfix increment operator and a variable, so it has those two effects:- The subexpression's value is
1
- the variable is updated:
d = 2
- The subexpression's value is
- right operand
++d
. This subexpression consists of a prefix increment operator and a variable, so it has those two effects:- The variable is updated:
d = 3
- The subexpression's value is
3
- The variable is updated:
- operator
+
is evaluated, using the values of the two operands.- Thus the expression value is 1 + 3 = 4.
The different precedence between the prefix and postfix forms of ++
would only be seen in ++d++
, which will be interpreted as ++(d++)
- and this has no meaning ((++d)++
has no one, either), since ++
only works on variables, not on values (and the result is a value).
This is not about precedence, it's about evaluation order. d++
evaluates to 1
, but then d
is incremented. ++d
increments d
, and then evaluates to 3.
See Why is this Java operator precedence being ignored here?.
It boils down to the fact that the postfix operator is being evaluated first, but returns the original value of the variable, as designed. So, for the purposes of your operation:
(d++ + ++d)
Processes as:
1. d++ evaluates, returning the original value of 1 but incrementing d to 2
2. ++d evaluates, incrementing the value of 2 TO 3, and returning 3
3. + evaluates, resulting in 1 + 3
The confusion is not in the order of precedence for the tokens to be evaluated, you've got that right. The real problem is in the understanding of the functional difference between the postfix and prefix operators.
d has value 1
d++ is evaluated; it's value is 1 and d is now 2 (post++ returns value before increment)
++d is evaluated; it's value is 3 and d is now 3 (++pre returns value after increment)
1 + 3 = 4
System.out.println(d++ + ++d);
Here's how it goes:
++d is executed, so d is now 2.
d + d is executed, which equals 4.
The value 4 is given to System.out.println()
d++ is executed, so now d is 3.
I went through all the explanations from top ..According to understanding following code should give 11.0 then y it gives 10.0 double x = 4.5; x = x + ++x; // x gets the value 10.0.
In addition to the other comments, I suggest you have a look at sequence points, as some of this stuff can lead to undefined behaviours, though I think your case is defined for java.
What does x[i]=i++ + 1;
do?
http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html
精彩评论