aspose file tools*
The moose likes Beginning Java and the fly likes Prefix postfix increment question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Prefix postfix increment question" Watch "Prefix postfix increment question" New topic
Author

Prefix postfix increment question

Montgomery Buslingthorpe
Greenhorn

Joined: Jul 24, 2012
Posts: 4


This code should be very simple, but somehow I am failing to understand why it prints -2. I seem to be missing something important about postfix and/or prefix increments.
Could somebody enlighten me here please ?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Consider this:

and see if it helps.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Montgomery Buslingthorpe
Greenhorn

Joined: Jul 24, 2012
Posts: 4
Ahhh, breaking it up like that, explains a lot :P

That results in -1 += -1 / 1

Thanks Greg

I've read a couple of times where people were saying that var++ is only incremented after the statement but to my understanding, the statement is var += var++/++var;

Breaking it up implies to me that var++ is incremented right after the / but before the ++var, (and therefore within and not after the statement).

If breaking up the parts like you mentioned, can be applied as a general rule of thumb, then these kind of lines would make a lot more sense to me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Montgomery Buslingthorpe wrote:. . . people were saying that var++ is only incremented after the statement . . .
That is an over‑simplification, and very inaccurate.
The truth of the matter is, there are two values going. The value of var which is 0 just before the / and the value of var++. The value of var++ is the same as the old value of var, i.e. -1.
The same applies to the second half of the expression. There is a value for var and a value for ++ var, but they are both the same at (+)1.
If you search these fora, you will find many similar questions; this causes confusion for everybody.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19004
    
  40

Montgomery Buslingthorpe wrote:
I've read a couple of times where people were saying that var++ is only incremented after the statement but to my understanding, the statement is var += var++/++var;

Breaking it up implies to me that var++ is incremented right after the / but before the ++var, (and therefore within and not after the statement).

If breaking up the parts like you mentioned, can be applied as a general rule of thumb, then these kind of lines would make a lot more sense to me.



Think of pre/post increments as having a side effect.... with post increment, the side effect happens right after the variable is used, and with pre increment, the side effects happens right before the variable is used.

The other thing to remember is that these side effects happen during evalution of the expression. This means that it happens based on the order of evaluation -- and not based on precedence and associativity. For your example, it makes no difference, but throw in a few more pre and post increments, and a few parens, and you can see the difference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

The most important thing to remember about the expressions a++ and ++a? Remember not to use them.

You may have to learn how they work if you're taking those certification exams, but trying to actually use them is going to be more trouble than it's worth. As you've already seen, trying to figure out the value of an expression which includes a++ or ++a in it is not easy. And you shouldn't write code which is not easy to understand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prefix postfix increment question