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

a simple question

 
andy lau
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=0;
i=i++;
System.out.println(i);

why the result is zero???
 
brent spearios
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ++ is evaluated after the variable is asigned the value if you put the ++ before the variable then you would get the desired results
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brent,
That's not exactly true.
++ is evaluated before the assignment but the value being used for the assignment is the one before the incrementation. But the bottom line is, ++ is evaluated before =.If ++ was evaluated after, we would have 1 as output.
andy,
you should maybe do a search in this forum since this topic has been discussed 1 gazillion times.
http://www.coderanch.com/t/191679/java-programmer-SCJP/certification/Maha-trick-hairy
http://www.coderanch.com/t/236547/java-programmer-SCJP/certification/maha-anna-topic-operators
[ April 12, 2002: Message edited by: Valentin Crettaz ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, short and sweet.
You have to understand the concept of a "return value" of a subexpression.
Given i++, the return value of this would be the original value of i prior to incrementation; on the other hand, for ++i the return value would be the incremented value of i.
The = operator has the lowest priority, so everything on the RHS (right hand side) would have to be resolved first, and the value on the LHS (left hand side) would have to be the return value of the RHS.
In the example above, the RHS went first so i was really incremented to 1, but the return value is 0. The LHS therefore receives the value of 0, overwriting whatever previous value was there.
The same argument goes whenever you pass a subexpression to a method as a parameter. The value of the parameter is whatever the return value of the subexpression.
For example, given

The value of i here is 1, but println() prints out the return value of i++, which is 0.
-anthony
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Two Examples:
1.-
int i = 0;
System.out.println(i++); //prints 0
2.-
int i = 0;
System.out.println(++i); //prints 1
It depends on the position of the incrementor, ++. This also aplies to "--".
hope it helps
 
Arun Pai
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test7
{
public static void main(String args[])
{
int var = 20, i=0;
do
{
while(true)
{
if( i++ > var) break;
}
}while(i<var--);
System.out.println(var++);

}
}
This code prints 19 and not 20.

-Arun
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic