Question

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.

Was it helpful?

Solution

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...

OTHER TIPS

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;
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

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top