+ 1
What is the difference between ++x and x++ in java? Please give the output of both also if x=6.
Question says it all.
3 Answers
+ 5
1. ++x indicates a prefixed increment. Here the.
incremented value of x will be available.
x=6;
y=++x;
/*
implemented as
x = x + 1;
y = x;
*/
System.out.println (x); // outputs 7
System.out.println (y); // outputs 7
2. x++ indicates a postfix increment. Here value of x will be used before increment.
x=6;
y=x++;
/*
implemented as
y = x;
x = x + 1;
*/
System.out.println (x); // outputs 7
System.out.println (y); // outputs 6
Hope this helps
+ 1
thanks @devender mahajan
0
Thanks @DevenderMahajan