File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment operator in assignment

 
Selvakumar Arumugam
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody please explain what is happening here?
--------------
int i =0;
i = i++;
i = i++;
i = i++;
System.out.println(i);
--------------
I was expecting i to be 3. But it's printing 0.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Selvakumar and welcome to the ranch, the post increment operator always returns its old value then increment it.
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is that the ++ operator is a postfix operator.
You can read x++ also as x = x = (x+1)
This means that the value of x in your case 0 is first assigned to x and then incremented. The value is incremented after the assignment operation.
The second time around the value of i will be still 0 and then assigned first and incremented etc.
 
Selvakumar Arumugam
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vicken and Thomas Thanks for the reply. I am clear about it.
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic