This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Basics

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
i'm having a hard time understanding this code :
int c = 0;
c += c++;
System.out.print ( c );
result is equal to 0 and not 2 has i was thinking.
can anybody explain me why ?
best regards
Lu�s Meira
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
luis
c += c++ is equivalent to:
c = c + c++
Before the operation is carrried out all of the operands are fully evaluated, c is equal to 0, and the result of the postfix increment is the original value of the operand so the value of c++ is also 0.
The reason it prints 0 then instead of 1 (or 2 as you thought) is becasue even the though the c++ increment sthe value by 1 that value is immediately replaced by the 0 that is assigned to c.
hoep that helps, if you are having confusion on the postfix operator there are plenty of threads on it in this forum, just do a search for it.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int c = 0;
c += c++;
System.out.print ( c );

c=0 therefore c+=c++; is the same as saying c=0+0;
because the ++ is done after the evaluation, if you did c+=++c; you would get 1.
 
luis meira
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by brent spearios:
int c = 0;
c += c++;
System.out.print ( c );

c=0 therefore c+=c++; is the same as saying c=0+0;
because the ++ is done after the evaluation, if you did c+=++c; you would get 1.


that's what i don't understand if ++ is avaluated
last by the time i do System.out.print ( c );
c should be 1.
i was thinking something like :
c = 0 + 0;
c++;
System.out.print ( c );
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's important to remember what's in the variable c while each piece of the expression is being evaluated. Let's go through it, step by step.
The code as is:

The first thing the JVM is going to do is evaluate each argument in the expression. I'll do this piece by piece and show what c is at each step. Let's start with the fact that c += c++ is equivalent to c = c + c++.

As you can see, in the line labelled "Look here" the expression c++ is evaluated to 0 and then c is incremented to 1. Now, however, if you continue with the evaluation of the expression, the right hand side evaluates to 0. This value is then assigned to c, which overwrites the value of 1 that was just there.
I hope this helps,
Corey
 
luis meira
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Corey McGlone:
It's important to remember what's in the variable c while each piece of the expression is being evaluated. Let's go through it, step by step.
The code as is:

The first thing the JVM is going to do is evaluate each argument in the expression. I'll do this piece by piece and show what c is at each step. Let's start with the fact that c += c++ is equivalent to c = c + c++.

As you can see, in the line labelled "Look here" the expression c++ is evaluated to 0 and then c is incremented to 1. Now, however, if you continue with the evaluation of the expression, the right hand side evaluates to 0. This value is then assigned to c, which overwrites the value of 1 that was just there.
I hope this helps,
Corey



now i think i got it.
you've made me see the light.
many thanks Corey,
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!