• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer Mistry  RSS feed

 
R Uppal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i m Rizwan Ahmed Uppal i face a problem during coding. problem is
Execute the code
int i=0;
i=i++;
System.out.println(i);

it should print 1 but it never prints 1 it always print 0.because one time i increment but answer is still amgious.I dont know y?.Is there some one have anwser?
sorry u all foks my question is that i=i++; is a complete statement and if it executed then i must be incremented one time i m printing i's value after increament.so it should be 1.

[ December 18, 2002: Message edited by: Rizwan Ahmed Uppal ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=0;
Assigns 0 to the variable i.
i=i++;
The right hand side increments the variable i by one, giving it the value 1. Since you used the increment postfix operator, the value of the right hand side is the value before the increment, i.e. 0. This value is then assigned to the variable i.
Remove "i=".
- Peter
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
That's the basic fundamental of postfix and prefix operator. The unary operator u have tried is postfix. It'll never give the value 1 for the first time.If u want the result to be 1, then use ++i instead of i++.
Regards
Nayan.
 
Zkr Ryz
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A ) i is gonna get some value, dont know wich,
B ) The value i has is 0 ( this is the value A is going to get)
C ) i is incremented by 1 thus i == 1
now that everiting is evaluated the assignations take place...
i ( A ) takes the value calculated in ( B ) which is 0
Now
printing i result on 0

Thats how I see it
=============
Zkr Ryz
[ December 19, 2002: Message edited by: Zkr Ryz ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!