• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

++ operator query

 
Mike Green
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int c = 1;
c = c++;
System.out.println("c = c++ .... : "+ c );

The output will be 1.

So is the increment operator ignored or discarded then?
I understand that c has been assigned the value 1 to give the final result but isn't c then incremented afterwards as well by the ++ operator?

I know this ISN'T the case but this is what I'm trying to figure out in my mind. So is the operator ignored or discarded then?


Cheers

Mike
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
Welcome to JavaRanch!
The "++" operator is really two different operators, depending on whether it comes before (prefix) or after (postfix) the variable it's operating on. The postfix version is the one that tends to get people a little confused, as you're experiencing first-hand!
First, here's how postfix ++ works: First, the value of the variable is read and stored. Next, the variable's value is incremented. Finally, the stored value is returned as the value of the expression.
Second, here's how assignment statements work in Java: the expression on the right hand side is evaluated, including all side-effects, and then the value of the expression is assigned to the variable on the left hand side.
So putting these together: to evaluate "c = c++", first the original value (1) of c is saved, and then c is incremented to 2. The stored value is taken to be the value of the expression on the right hand side, and so that value is put into c. So c starts out as 1, is incremented to 2, and then set back to 1. Of course, a smart compiler ought to be able to optimize this out.
Got it?
 
Amit Ghai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also try this link out. This contains a health discussion of the same.
http://www.coderanch.com/t/244938/java-programmer-SCJP/certification/post-increment-confusion
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi mike,
in your'e example,
int c = 1;
c = c++;
System.out.println("c = c++ .... : "+ c );
we could look at it this way. lets make one of the c's bold
int c=1;
c = c++; //think of first c as a new variable.
System.out.println("c = c++.... :"+ c);
we could make the first c a different letter to make it easier.
d = c++; so the post incrementer only increments when the whole statement is finished.
so really d = c;//statement terminates here, so then increment.
//but because d has been assigned, it cannot see what c got incremented to.

Amit Ghai has provided a link to one of the best discussions on this topic. everyone has a different way of looking at it, and if it works for you then use it.
Davy
 
Mike Green
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant. Its all clear now thanks for everyones explanations and I also went to see the other posts. I found a good way of writting it down as well.
int c=1;
c = c++ + ++c + c + c++;
System.out.println(c);
Answer is 10.
I found from an earlier post someone using brackets. So:
c = c++ + ++c + c + c++;
c = 1(2) + (3)3 + 3 + 3(4);
Where the brackets go on one side of the actual value if its a post incrementer and the other if its a pre incrementer.
I also now understand that the last increment is discarded in this particular example.
Thanks for your help
Mike
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well done mike, now you can make up larger variations of this problem, to see if you fully understand.
maybe say c=192;
c=c++ + c++ + ++c + c++;
davy
 
Mike Green
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Davy
that would be:
c = 192(193) + 193(194) + (195)195 + 195(196);
c is now 775.

What about:
int c = 845;
c = c++ + c++ + c++ + c + c++ + ++c + c++;
c=845(846)+846(847)+847(848)+848+848(849)+(850)850+850(851)
answer 5934.
Just checked it in a program and I am correct.

Its a quick way of doing it as well. Whenever I tried to do these things before I ended up trying to count 2 values up in my head and it all got very confusing indeed.
Nice One
Mike
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well done, you have got the hang of it, with this knowledge you will romp these question in future
davy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic