File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes int c=1; c=c++;  // c is then 1 not 2 ! 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 "int c=1; c=c++;  // c is then 1 not 2 !" Watch "int c=1; c=c++;  // c is then 1 not 2 !" New topic
Author

int c=1; c=c++; // c is then 1 not 2 !

Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
int c = 1;
c = c++; //c is then 1,not 2. ++ is after c!
Isn't
c = c++;
same as
c = c; // c is still 1.
c++; // c is then 2.
?
I was so confused !
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
This topic has been heavily discussed. After a short search I have been able to come up with the following useful links:
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array
http://www.coderanch.com/t/191291/java-programmer-SCJP/certification/Its-hairy-again
http://www.coderanch.com/t/236547/java-programmer-SCJP/certification/maha-anna-topic-operators


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
Originally posted by Alan Chong:
int c = 1;
c = c++; //c is then 1,not 2. ++ is after c!
Isn't
c = c++;
same as
c = c; // c is still 1.
c++; // c is then 2.
?
I was so confused !


c=c++ ;
u can assume the above line of code as below
c=1 ( post-increment-->first assign and the increment)
1++;
print c// prints c=1;
HTH
murthy


MT
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Murthy, be aware that 1++ is not authorized in Java.
manasa teja
Ranch Hand

Joined: May 27, 2002
Posts: 325
Originally posted by Valentin Crettaz:
Murthy, be aware that 1++ is not authorized in Java.


Yes Valentin !!!
Just to solve this problem, I assuemed like that as I also got confused initially. My intention was that c will not be incremented after c=1 in c=c++;

thanks
Murthy
Deepali Pate
Ranch Hand

Joined: Mar 20, 2002
Posts: 114
int x=1;
int y;
y=x++;
System.out.println(y);//value 1 is printed
System.out.println(x);//value 2 is printed
This shows that the value of x is first assigned to y and then incremented just as it reads x++.
If it was other way round ie. ++x then
int x=1;
int y;
y=++x;
System.out.println(y);//value 2 is printed
System.out.println(x);//value 2 is printed
Then it is first incremented and then assigned to y. Just as it indicates ++x.
One tricky ? i had seen on this topic and good one for beginners is
If int x=3 and int y=4. Which is true after y=x++;?
x=y;
x>y;
x<y;
Correct ans is x>y.
coz y=x sets value of y to 3 and the increments x so x=4 and hence x>y.
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi Deepali,
The important point here is that the variable
assigns a itself to itself in one single statement !
Alan Chong
Ranch Hand

Joined: Jun 05, 2002
Posts: 106
Hi all,
I think I got the answer for myself.
The memory chip cannot be read and written at
the same time.
In the case of c=c; ( c is memory location)
Because CPU cannot transfer from a memory location to another location(or the same
location) directly, value in c is copied to a register in the CPU, and the value of that register is then copied back to c.
In the case of c=c++; // assume c is 1.
Value in c is copied to a register in the
CPU before c is incremented. After doing the
copying, the CPU increments c, making c equal
to 2. Finally the assignment operator make the CPU copied the value in that register back to
c. The value in that register is 1 and so it
overrides value 2 in c which was done by the above increment.
Intel CPUs actually have instructions which can transfer data from memory to memory in ONE STEP, with the help of some internal registers. But Sun cannot assume that for portability reason. So the TWO STEP way are used.
If c=c is done in one step, then c=c++;is same as
c=c;
c++;
It depends on Sun, but Sun chose to increment
c immediately after value in c is copied to a CPU register.So this increment gets widged in!
If Sun have chosen to increment c after the whole statement, then everything will get back to rational state.
*Maybe it is much easier to maintain the code by incrementing c immediately after value in c is copied to a CPU register than to do that
after the whole statement is finished.
( A statement could easily yield more than one hundred CPU instructions!)
Those who have assembly background,how do you thing about my speculation ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Alan Chong:
Those who have assembly background,how do you thing about my speculation ?

I think you're making this way more complicated than it is. This wasn't a decision based on registers or processors. Nor is this a bug. This is the way Java works. It was intended to function this way.
The key is to understand how expressions are evaluated and how the post-increment operator functions. If you understand these things, you'd see that it would actually be strange for x=x++ to increment x. Rather, it should assign the original value to x, which is what occurs.
Corey


SCJP Tipline, etc.
 
 
subject: int c=1; c=c++; // c is then 1 not 2 !