aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion ..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion ....." Watch "confusion ....." New topic
Author

confusion .....

udit agarwal
Greenhorn

Joined: May 25, 2006
Posts: 9
hey friends....

i m going to write the Scjp 1.4 on 10th june...
i have a confusion regarding to the code below..

int i;
i=i++;

wat will be the value of i

First when i saw it..i think that the ootput will be 1 but.i was shocked when the output show 0....

then i run the same code on c and c++ compiler then it shows 1...

so can any one tell me why it show 0.....

Udit Agarwal


Good Designer is alwasys a good programmer. Trying to improve designing skill...
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
Hi,

The key here is to understand how the post-increment operator works as well as the order in which expressions are evaluated.

The post-increment operator works in this way -
1. return the current value of i (= 0)
2. increment the current value and store it in variable i. (i = 1)

Now, the value returned by the post-increment operation (i.e. 0) is assigned to the LHS variable i, overwriting the previously written value 1.

Thus, i = i++; leaves i = 0;

Hope, this has cleared ur doubt.


-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
udit agarwal
Greenhorn

Joined: May 25, 2006
Posts: 9
hey..

Nice to see the answer......
but why it shows 1 when complile in c and c++

Is the post increment operator works differently in c anc c++
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
I m not exactly aware about C & C++.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
This Thread


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
I posted the same doubt couple of weeks back. I never got any answer. The i=i++ statement works differently in C and C++. It gives the answer 1. But my reasoning is that post-increment operator is one of the fundamental operators and its behaviour should be the same irrespective of implementation language. I understand the working of i++ statement in Java, but I am still not convinced!!! And I believe that's why most of the people find it so hard to understand the output in Java as majority of us come to Java after a stint in C and C++.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
yeah dats true, one thing u must remember, c,c++ and java are totally different even at operator level.
So never mix them
neetika sharma
Ranch Hand

Joined: May 19, 2006
Posts: 89
i=i++;

I got some explantion like this, quite convinced I was with this so sharing it....


What happens with that code is this:

Let's say 'i' has a starting value of 0. The expression 'i++' evaluates to the pre-increment (original) value of 'i', or 0. The result of this expression (0) is remembered. The value of 'i' is then incremented ('i' is now 1). The '=' assignment operator has the least precedence, so it happens last: the result of the right-hand expression (what we remembered earlier, namely 0) is assigned back to 'i'. Therefore, the result of the whole statement is that 'i' has a value of 0.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion .....