0

New to JAVA, and I know there have been some discussions about ++i and i++, but I still don't know how to approach the question below and how to explain the answer. I shared what I think, but I hope you can help correct what's wrong with my thinking. Thank you very much!

The question is:

Give the results of the following java program.

int z = -1;

system.out.println(++z);

(My thinking: pre-increment, which is incrementing the value of i by 1, so the output is -1+1 = 0)

system.out.println(z--);

(My thinking: pre-decrement, which is decrementing the value of i by 1, so the output is -1-1 = -2, but why is 0? )

system.out.println(z++ + z);

(My thinking: post-increment, which is incrementing the value of i by 1.So the entire thing reads as (z++ plus z). So z++ is 0, + (plus) z, which is 0 + (-1), so the output is -1)

system.out.println(z + z++);

(My thinking: the entire thing reads as (z plus z++). So z is -1, plus z++, which is 0, so the output is -1, but the answer is 0.)

The answer is: 0 0 -1 0

What the answer would be if we changed every + to -?

int z = -1;
system.out.println(--z);
system.out.println(z-- - z);
system.out.println(z - z--);

After reading lots of discussions, I still feel confused. Really hope to learn to understand the concepts. Thank you!

user13966876
  • 55
  • 1
  • 5
  • _"My thinking: pre-decrement, which is decrementing the value of i by 1, so the output is -1-1 = -2, but why is 0?"_ - `z--` is a post decrement. So It will decrement the value of `z` _after_ printing its value. – Yousaf Dec 15 '21 at 06:16
  • _"My thinking: post-increment, which is incrementing the value of i by 1.So the entire thing reads as (z++ plus z). So z++ is 0, + (plus) z, which is 0 + (-1), so the output is -1"_ - Before the evaluation of `z++ + z`, value of `z` is `-1`. So `z++ + z` will be evaluated as `-1 + 0` which equals to `-1`. (Note that value of `z` after the `+` operator is 0 because at that point it has already been incremented) – Yousaf Dec 15 '21 at 06:20
  • _"My thinking: the entire thing reads as (z plus z++). So z is -1, plus z++, which is 0, so the output is -1, but the answer is 0"_ - Before the evaluation of `z + z++`, value of z is `0`. So `z + z++` will be evaluated as `0 + 0` which equals to `0`. (Note that value of z is incremented _after_ the addition has already been done) – Yousaf Dec 15 '21 at 06:23

2 Answers2

1

(My thinking: pre-decrement, which is decrementing the value of i by 1, so the output is -1-1 = -2, but why is 0? )

Its not pre-decreement, its post-decreement. For clarity :

  • ++a -> pre-increement
  • a++ -> post-increement
  • --a -> pre-decreement
  • a-- -> post-decreement
int z = -1;
System.out.println(++z);
// ++(-1) => 0; z = 0
System.out.println(z--);
// (0)-- => 0; z = -1
System.out.println(z++ + z);
// (-1)++ + (0) => -1; z = 0
System.out.println(z + z++);
// (0) + (0)++ => 0; z = 1

What the answer would be if we changed every + to -?

I think it would be a nice exercise for you, try it on our own and check by writing a java program for it

Sarvjot
  • 33
  • 7
  • Thank you so much for your help! I tried to answer my own question about if I change "+" to "-". But please correct me if I am still not doing it right. int z = -1; System.out.println(++z); // ++(-1) => 0; z = 0 System.out.println(z--); // (0)-- => 0; z = -1 System.out.println(z++ + z); // (-1)++ + (0) => -1; z = 0 System.out.println(z + z++); // (0) + (0)++ => 0; z = 1 – user13966876 Dec 16 '21 at 15:53
  • It seems you have quoted the original question itself rather than replacing every `+` with `-` – Sarvjot Dec 18 '21 at 04:50
0

The major difference between per-increment and post-increment operation is that the time assignment takes place. in pre-inc operation increment happens before operation. but in post increment first operation then increment. Try to run the below code.

System.out.println(++z);// ++z means z=z+1 hence o/p 0
    System.out.println(z--); here first print value 0. then increment happens
    System.out.println(z);//here you will get the value -1
    System.out.println(z++ + z);//now the value of z is-1 value inc+z 's value
//-1+0 will give -1
    System.out.println(z);// assignment happens later here value is 0
    System.out.println(z + z++);//0+0=prints 0 later increment
    System.out.println(z);// final value for z is 1
Soniya Mohan
  • 4,514
  • 2
  • 12
  • 14
  • int z = -1; system.out.println(--z);//-2 system.out.println(z-- - z);//1 system.out.println(z - z--);//0 if you have further doubt try to print value of z after every operation you will get a clear picture. – Soniya Mohan Dec 15 '21 at 07:14