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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…