+ 2
In java, what is the difference between ++x and x++. And what moment is important to use x++ ?
x is a variable (exemple x=10;) what is the difference between ++x and x++. And what moment is important to use x++ ?
4 Answers
+ 3
You have the int x = 10;
Then you write:
int y = x++;
Now y = 10 and x = 11
Lets define x = 10 again, then you write:
int y = ++x;
Now y = 11 and x = 11
++ was applied after set the value of y, and then ++ was applied before.
+ 27
int a=10;
++a; // or a++; or int y=++a; or int y=a++;
System.out.print(a); //11 will be printed in all cases
//but if ...
int a=10;
a=a++;
System.out.print(a); //10 will be printed
//and if
int a=10;
a=++a;
System.out.print(a); //11 will be printed
+ 3
thanks
+ 2
the basic difference between ++x and x++ is,
in ++x increment is done then value is assign:
for example:
x = 1 and
y = ++x
first increment is done then value is assign to y
i.e
x = 2, then y = 2
and
y = x++
first value is assign then increment is done in x
i.e
y = 1, x = 2