• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic has been heavily discussed. After a short search I have been able to come up with the following useful links:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
https://coderanch.com/t/191291/java-programmer-SCJP/certification/Its-hairy-again
https://coderanch.com/t/236547/java-programmer-SCJP/certification/maha-anna-topic-operators
 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Murthy, be aware that 1++ is not authorized in Java.
 
manasa teja
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepali,
The important point here is that the variable
assigns a itself to itself in one single statement !
 
Alan Chong
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic