File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variables

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • 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
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic