-1
int x=0;
x=x++;

System.out.println("The Value Of X-->"+x);

It will print 0 but why?

If we write the following code,

int x=0,y=0;
y=x++;

System.out.println("The Value Of Y-->"+y);
System.out.println("The Value Of X-->"+x);

Then the output will be 0 and 1. My question is in my first code first I put the x value to 0 then increment x so the x which value is 0 is replaced by 1,so the result should be 1 but here we got result is 0.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Rajib
  • 7
  • 1

1 Answers1

1
x = x++

Increments x but then it returns its old value.

You should only do

x++

to increment x by 1.

Andrea
  • 6,032
  • 2
  • 28
  • 55