Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variables

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
i want why the output is different in java for the below code
---
int a=2;
a=a++;
System.out.println(a);
---
the output is 2
why
&how the code is executed(i want some algor)
thank u
srinivasan.e

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because the expression
a++
is evaluated after the assignment.
a is assigned a and then a is increased by 1.
Instead use only the statement a++ or a=++a instead.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic