File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ++ operator query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "++ operator query" Watch "++ operator query" New topic
Author

++ operator query

Mike Green
Greenhorn

Joined: Apr 06, 2004
Posts: 7
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
Amit Ghai
Ranch Hand

Joined: Mar 24, 2004
Posts: 35
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


Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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


How simple does it have to be???
Mike Green
Greenhorn

Joined: Apr 06, 2004
Posts: 7
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

Joined: Jan 12, 2004
Posts: 384
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

Joined: Apr 06, 2004
Posts: 7
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

Joined: Jan 12, 2004
Posts: 384
well done, you have got the hang of it, with this knowledge you will romp these question in future
davy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ++ operator query