Pre- and postincrement in Java
Asked Answered
T

6

11

I just wanted to create a little Java-Puzzle, but I puzzled myself. One part of the puzzle is:

What does the following piece of code do:

public class test {
    public static void main(String[] args) {
        int i = 1;
        i += ++i + i++ + ++i;

        System.out.println("i = " + i);
    }
}

It outputs 9.

My (at least partly) wrong explanation:

I'm not quite sure, but I think the term after i += gets evaluated like this:

enter image description here

So

int i = 1;
i += ++i + i++ + ++i;

is the same as

int i = 1;
i += ((++i) + (i++)) + (++i);

This gets evaluated from left to right (See Pre and postincrement java evaluation).

The first ++i increments i to 2 and returns 2. So you have:

i = 2;
i += (2 + (i++)) + (++i);

The i++ returns 2, as it is the new value of i, and increments i to 3:

i = 3;
i += (2 + 2) + ++i;

The second ++i increments i to 4 and returns 4:

i = 4;
i += (2 + 2) + 4;

So you end up with 12, not 9.

Where is the error in my explanation? What would be a correct explanation?

Toadflax answered 11/7, 2012 at 11:43 Comment(3)
Are you sure it outputs 8? cause for me it outputs 9.Agnes
Thanks for the hint. It outputs 9. (8 was one of the other puzzles). Nevertheless, 9 is not 12.Toadflax
possible duplicate of Pre and postincrement java evaluationMeathead
A
10

i += ++i + i++ + ++i; is the same as i = i + ++i + i++ + ++i;

The right-hand side is calculated from left-to-right, yielding i = 1 + 2 + 2 + 4; (which yields i = 9).

Apprehensible answered 11/7, 2012 at 11:51 Comment(3)
So my thoughts were correct, but i += s gets evaluated to i = i + s which is NOT equivalent to i = s + i, where s is a statement. Correct? Do you have a source for the fact that i += a gets evaluated to i = i + a?Toadflax
@moose It is all in Java language spec: docs.oracle.com/javase/specs/jls/se7/html/…Mythicize
@jsn Thanks for linking for me. moose Good question here. Situations like these are where the Mathematics gets really fuzzy if you don't know the specification or where to look. Cheers.Grapevine
P
3

You're right regarding the right part evaluation, but you're missing a detail regarding the assignment.

Run this :

i = i++;

or this :

i += i++;

After both operations, i still has its original value.

That's because i is evaluated on the left before the right part of the assignment.

So in your case, you're adding 8 to 1, not to 4.

Prostitute answered 11/7, 2012 at 11:49 Comment(0)
H
3

The output is 9 (try it)

int i = 1;
i += ++i + i++ + ++i;

becomes

i = 1 + 2 + 2 + 4
Husein answered 11/7, 2012 at 11:49 Comment(0)
S
1

it's very easy to understand how it works if you imagine it how java stores values in registers! he puts 1 in the first register, and than goes through = sign, and increments the i(++i), so now in i you have 2, and in the second register you have 2, but the first register is not updated, in the third register you'll have 2 and then i is incremented, and then i is incremented and in the last register you'll have 4. So you'll have something like this 1 = 2 + 2 + 4 == 9

Stromberg answered 11/7, 2012 at 11:55 Comment(0)
L
1

The code

int i = 1;
i += ++i + i++ + ++i

is equivalent to

int tmp1 = i // 1, +=

i ++; // 2
int tmp2 = i; // 2

int tmp3 = i; // 2
i ++; // 3

i ++; // 4
int tmp4 = i; // 4

i = tmp1 + tmp2 + tmp3 + tmp4; // 9
Lynnelynnea answered 11/7, 2012 at 12:1 Comment(0)
V
0

i += ++i + i++ + ++i;

  1. i=1 at start
  2. i += X -> i = i + X -> i = 1 + X (so lets count X)
  3. ++i will be incremented to 2 and return 2
  4. i++ will return 2 and then be incremented to 3
  5. ++i will be incremented from 3 to 4 and return 4
  6. X = 2 + 2 + 4 = 8

So i = 1 + 8 -> i=9


You would get 12 if your code would be something like this

int i = 1;
int tmp = ++i + i++ + ++i;  
i += tmp;

because then your code would be i=1, and after calculating tmp i would be i=4, then i+=tmp -> i=4+8=12

Virtually answered 11/7, 2012 at 12:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.