2
int z = 1;
System.out.println(z++ == ++z);
System.out.println(++z == z++);

the output will be:

false
true

and I don't get why, please explain this to me.

user827992
  • 1,743
  • 13
  • 25
  • 3
    Duplicate of [explain working of post and pre increment operator in Java](http://stackoverflow.com/questions/2371118/explain-working-of-post-and-pre-increment-operator-in-java) – DaoWen Aug 20 '12 at 11:06
  • @DaoWen Not exactly a duplicate - when a statement contains several pre/post increment operators, the order in which each operand is executed is what matters (assuming one understands the difference between a single ++x vs x++). The question you point to uses the additive operator (which happens to also be left-associative), not the equality operator. – assylias Aug 20 '12 at 11:19
  • @assylias - I guess that's true. I think "explain working of post and pre increment operator in Java" is a better title for this question though. :) – DaoWen Aug 20 '12 at 11:24

6 Answers6

10

Operands of == are evaluated left to right, and the ++ has higher priority, so your code is equivalent to:

int z = 1;
int tmp1 = z++; //tmp1 = 1 / z = 2
int tmp2 = ++z; //tmp2 = 3 / z = 3
System.out.println(tmp1 == tmp2);

tmp1 = ++z; //tmp1 = 4 / z = 4
tmp2 = z++; //tmp2 = 4 / z = 5
System.out.println(tmp1 == tmp2);

I assume you understand the difference between z++ and ++z:

  • tmp1 = z++; can be broken down into: tmp1 = z; z = z + 1;
  • whereas tmp2 = ++z; can be broken down into: z = z + 1; tmp2 = z;
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    If you're going to break it down and use `tmp` vars to hold the intermediate values, maybe you should show what the pre- and post-increment operators actually expand to. – DaoWen Aug 20 '12 at 11:11
5
int z = 1;
    System.out.println(z++ == ++z);
    System.out.println(++z == z++);

z++ is post increment and ++z is pre-increment. Post increment increases the value after the expression is evaluated and pre increment increase the value before the expression is evaluated.

Hence,

int z = 1;
    System.out.println(z++ == ++z); // 1 == 3 false
    System.out.println(++z == z++);// 4 == 4 true
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
3

the operator == gives precedence to what is on its left, in other words since it operates on 2 values, the values on the left is evaluated first.

the operator ++ before the label of the var indicates that the increment is assigned to the value before evaluating it, the same operator putted after the label cause a post-increment, meaning that the value of the variable is incremented by 1 after evaluating it.

regarding the second row of your code:

  • the operator == looks on its left
  • the operator evaluate z as 1
  • the operator has done with the left part but an increment is made because of your z++ that is just saying that, increment after z is evaluated, so now z is 2 but is evaluated by the == operator as 1, remember that.
  • the operator == looks on the right
  • the operator can't evaluate what is in z without making an increment because of ++z, this means that z is evaluated as 3

1 != 3.

same concepts applies to the next row.

user827992
  • 1,743
  • 13
  • 25
0
System.out.println(z++ == ++z); // 1 == 3 is false
System.out.println(++z == z++); // 4 == 4 is true

While using post increment value is copied into temporary variable 'tmp', check here postincrement. So z++ == ++z => evaluates to 1 == 3 which is false. Now z is 3.

Coming to 2nd expression : ++z == z++, again : ++z becomes 4 and in case of z++, value is copied in tmp variable and used it at z++. Expression becomes 4 == 4 which is true and final value of z is 5

Community
  • 1
  • 1
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • 3
    Didn't downvote, however you've added no real explanation as to -why- this happens. Assume the OP doesn't understand pre-inc or post-inc operators. =) – J. Steen Aug 20 '12 at 11:11
0

First You Should Clear About Pre Increment & Post Increment Lets Have A Look

int i=1;
int j=i++;
int k=++i;

here in the value of j will be 1 and k will be 3

why means in i++ first store the value i into j and after which increments the value of i so

j=1,i=2 ok :)

now in the case of ++i where first increment the value and after that store into k so

k=3,i=3 ok :)

now am coming to your question

int z = 1; System.out.println(z++ == ++z);

so tell me what will be z++ ? 1 ok:) hence ++z will be ? 3so the statement will print false ok :) and the second case

System.out.println(++z == z++);

++z will be 4 and z++ will be 4 so you got true So hope You Clear...

J.K
  • 2,290
  • 1
  • 18
  • 29
  • thanks jenuine :) i like ur answer the most,you explanation can clear even a baby too :) thanks :) –  Aug 21 '12 at 10:41
0

The statement ++z returns the incremented value of z whereas z++ returns its previous value before increment, however the value of z after z++ expression returns is the incremented value of z.

so

int z = 1;                                        // Line 1
System.out.println(z++ == ++z);                   // Line 2
System.out.println(++z == z++);                   // Line 3

In line 2, z++ returns 1 and value of z is incremented once it returns so z is now 2 and ++z increments the value before returning so z is 3. Therefore line 2 is

System.out.println(1 == 3);

which is false.

In Line 3, ++z evaluates to 4 (since previous value of z is 3) and z++ again returns the previous value of z i.e. 4 but z is 5 now. Therefore Line 3 is

System.out.println(4 == 4);

which is true.

GauravLuthra
  • 1,027
  • 9
  • 8