0

I have a code snippet in Java:

 int y = ++x * 5 / x-- + --x;

So my confusion was since x--(postfix) has higher precedence than ++x(prefix) operator so x-- should be executed first then ++x.But a book states otherwise.Am I right in my thinking?

5
  • 5
    precedence != order of execution, see: blogs.msdn.microsoft.com/ericlippert/2008/05/23/…
    – Caleth
    Commented Oct 11, 2016 at 12:17
  • @Caleth This comment is important, and the only currently existing answer is wrong. You should turn your comment into a full answer. Commented Dec 7, 2018 at 8:46
  • 2
    doctor doctor it hurts when i do this...
    – jk.
    Commented Dec 7, 2018 at 8:49
  • Just curious. In C (and C++) this would be undefined behaviour. Does Java actually define the behaviour? Commented Dec 7, 2018 at 9:00
  • @NickKeighley yes, Java has a hatred of UB. It's left to right
    – Caleth
    Commented Dec 7, 2018 at 9:36

2 Answers 2

3

In the absence of any structures being indexed using these variables, which is where prefix and postfix operators really come into their own, I'd suggest that this is a largely academic exercise.

Speaking pragmatically; take control of the expression and add brackets to make the order explicit, if only to save your sanity. It also eliminates any possibility of confusion and/or portability issues (new compiler, [slightly] different operator precedence, nasty bug).

3
  • W.,I agree but if this code is analysed,then am I right in my approach Commented Oct 11, 2016 at 11:43
  • You are wasting your time analyzing this code.
    – user949300
    Commented Dec 7, 2018 at 6:46
  • Brackets don't change order of execution. Splitting the expression into separate statements does (and is a good idea). Commented Dec 7, 2018 at 8:42
0

Precedence determines which calculations will happen. Evaluation is done left to right among things that could occur "at the same time" in Java.

It is equivalent to the following

int t1 = ++x;
int t2 = x--;
int t3 = --x;
int y = t1 * 5 / t2 + t3;

If you have to care about either of those things, you've got bad code. Change it so that the reader doesn't have to consult their rulebook to know what is happening.

3
  • If there are "too many" ++s and --s in a line, the rules don't always apply (at least in C, not sure about Java). This surprised me, but was discussed in another SO question.
    – user949300
    Commented Dec 7, 2018 at 16:34
  • @user949300 Java is a completely different language to C. And "too many" is "any other mention of s, irrespective of operations involved"
    – Caleth
    Commented Dec 7, 2018 at 16:36
  • @user949300 C (and C++) explicitly doesn't have rules as to what occurs. If you have ++x + x, you don't have a C (or C++) program. In general, the compiler is not required to inform you of this.
    – Caleth
    Commented Dec 7, 2018 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.