+ 4

Why we use x++ in java?

public class Program { public static void main(String[] args) { int x = 10; int y = x++; System.out.println(y); } } //output is 10

28th Sep 2017, 11:17 AM
Injamul Mohammad Mollah
Injamul Mohammad Mollah - avatar
3 Answers
28th Sep 2017, 11:19 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
x++ means your incrementing value on x by 1 so x will be 11. In your example x=10 and y has assigned value of x which is 10 but the increment operator is on right which is called post increment which will increment value on next output of y. Try to output y two time to get 11 as output.
28th Sep 2017, 2:26 PM
Siddhesh Pednekar
Siddhesh Pednekar - avatar