+ 2

Stuck in Increment in Java!! (Shame on me😅) ... Write the output for the two cases

int x=2, y=2; Case 1:- x=x++; System.out.println (Wait...Loading....); System.out.println (Loading...); System.out.println (Finally loaded...); System.out.println ("x="+x+" y="+y); Case 2:- y=x=x++; System.out.println ("x="+x+" y="+y);

2nd Sep 2020, 7:50 AM
I M J
I M J - avatar
2 Answers
+ 3
In case 1, x is incremented by one, that is, x is now x = 3. So output is x=3y=2 Case 2, First x++ is implemented, so, it will return 2 as its post increment. It will return value, then increment. So, y = x = 2. So, y and x is 2 now. So output is, x=2y=2
2nd Sep 2020, 8:44 AM
Charitra
Charitra - avatar
+ 3
Charitra Agarwal understood
2nd Sep 2020, 9:07 AM
I M J
I M J - avatar