+ 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++ ?

8th Jan 2018, 3:09 AM
Bamogo Abdoul Aziz
Bamogo Abdoul Aziz - avatar
4 odpowiedzi
+ 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.
8th Jan 2018, 3:18 AM
Sebastián Zapata
Sebastián Zapata - avatar
+ 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
8th Jan 2018, 7:35 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 3
thanks
8th Jan 2018, 3:39 AM
Bamogo Abdoul Aziz
Bamogo Abdoul Aziz - avatar
+ 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
8th Jan 2018, 6:11 PM
JAFFAR JAWED
JAFFAR JAWED - avatar