(Potentially odd) long increment behavior?
Asked Answered
M

4

8

I'm a bit embarrassed in asking this question, but the result of the following code snippet has me stumped:

System.out.println("incrementResultResponses() has been invoked!");
final long oldValue = resultResponses;
final long newValue = resultResponses++;
System.out.println("Old value = " + oldValue);
System.out.println("New value = " + newValue);

That outputs the following:

incrementResultResponses() has been invoked!
Old value = 0
New value = 0

Why? Would concurrency have any influence upon the result here? By the way, resultResponses is a long.

Mussel answered 6/2, 2012 at 13:38 Comment(2)
possible duplicate of Strange behaviour of the increment operators in Java? - probably not the best dup, but there are literally hundreds. Please search for "[java] post-increment".Iorio
@Mat, Yes, my fault. If at all possible, I would like others to vote close this question as a duplicate.Mussel
G
13

The postfix ++ operator returns the old value (before incrementing). You want to use prefix ++:

final long oldValue = resultResponses;
final long newValue = ++resultResponses;
Grosso answered 6/2, 2012 at 13:40 Comment(4)
That's a bummer. For some reason, I thought assigning the postfix operator to a variable would force the operation to return the new value. :/Mussel
Why a bummer? This is just how postfix and prefix ++ are defined: postfix increments the variable and returns the old value, why prefix increments the variable and returns the new value. If you want the latter, use prefix instead of postfix ++.Grosso
Thats crazy. I know at least 5 other languages where number++ returns the new value immediately. I was wondering why it wasnt working in Java. Thanks to you guys for learning something newGopherwood
@DavidFariña really? Java has inherited this behavior from C and C++ and also in C# and JavaScript it works in the same way. So it's not a thing that is peculiar to Java.Grosso
H
3

Because the increment increases the value after it was assigned (Post-Increment). That's exactly what resultResponses++ is supposed to do.
If you want resultResponses to be 1, you need to use Pre-Increment, which is ++resultResponses

Higgledypiggledy answered 6/2, 2012 at 13:42 Comment(0)
S
2

If you want to increment oldValue before the assignment you will have to place ++ before the variable:

final long newValue = ++resultResponses;

This means that the increment takes place before the statement is executed instead of after.

Spandrel answered 6/2, 2012 at 13:41 Comment(0)
K
1

Refer this, to know how postfix and prefix work. As mentioned in the above answers you can use this:

final long oldValue = resultResponses;
final long newValue = ++resultResponses;

Or to make it fancier you can also use:

final long oldValue = resultResponses++;
final long newValue = resultResponses;

which will also result in the same output.

Kletter answered 1/2, 2018 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.