0
Why does x also increase when we print y?
public class Program { public static void main(String[] args) { int x = 34; int y = ++x; System.out.println(y); } } If I was to print x why does that also increase. It only makes sense for y to increase?
5 Answers
+ 1
ok i got it. ++x changes the value of x to x+1 and then it gets stored in variable y. So, x become x+1.
If you don't want x to change you should do x+1 instead of ++x.
+ 1
I dont know about Java much but doesn't ++x increment the value of x by 1? so it increases.
+ 1
Chris Welham first ++x changes x into x+1. This then gets stored in y.
0
Well the way i see it is i set y to be x + 1 but shouldnt x stay as 34
0
So y is just x and x is ++x