+ 2

Why it’s 70 and not 69?

Where is the mistake people, who can help? If x = 34 and y = ++x, then y supposed to be 35 which means x+y=69, no? When I run the code I receive an answer 70(( public class Program { public static void main (String [ ] args) { int x = 34; int y = ++x; int z = x+y; System.out.println(z); } } Thanks, Arthur

11th Feb 2018, 1:55 PM
Arthur
6 Respuestas
+ 14
int x=34; int y=++34 //y=35 as well as x=35 int z=35+35 = 70 //hope u got it ☺
11th Feb 2018, 4:43 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 12
https://www.sololearn.com/Discuss/407846/?ref=app this will give you some more information about increment decrement
11th Feb 2018, 2:25 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 3
The statement: int y = ++x; increments the value of x by one and then assigns the incremented value to y. It means that after this statement, both x and y contain the value 35. That's why the output is 70, not 69. If the statement was: int y = x+1; the output would have been the way you are thinking, because it does not increment the value of x so it remains 34 and y becomes 35.
11th Feb 2018, 2:02 PM
Deepesh Choudhary
Deepesh Choudhary - avatar
+ 3
For z = 69, write this: int x = 34; int y = x++; z = x+y; Now y is 34 and x is 35, because x gets incremented AFTER y copies its value.
11th Feb 2018, 5:26 PM
Chris
Chris - avatar
+ 1
the ++x increments x and then makes y=x x is 35 after the ++x and not 34 anymore
11th Feb 2018, 2:02 PM
Alex
Alex - avatar
+ 1
Thank you guys
11th Feb 2018, 2:04 PM
Arthur