5

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.

mre
  • 42,270
  • 33
  • 119
  • 166
  • 1
    possible duplicate of [Strange behaviour of the increment operators in Java?](http://stackoverflow.com/questions/7490790/strange-behaviour-of-the-increment-operators-in-java) - probably not the best dup, but there are literally hundreds. Please search for "[java] post-increment". – Mat Feb 06 '12 at 13:41
  • @Mat, Yes, my fault. If at all possible, I would like others to vote close this question as a duplicate. – mre Feb 06 '12 at 13:42

4 Answers4

12

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

final long oldValue = resultResponses;
final long newValue = ++resultResponses;
Jesper
  • 195,030
  • 44
  • 313
  • 345
  • 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. :/ – mre Feb 06 '12 at 13:41
  • 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 `++`. – Jesper Feb 06 '12 at 13:43
  • 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 new – David Fariña Jun 06 '19 at 07:25
  • @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. – Jesper Jun 06 '19 at 09:30
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

Simon Woker
  • 4,914
  • 26
  • 41
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.

Jivings
  • 22,276
  • 6
  • 54
  • 98
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.

Mukesh A
  • 321
  • 1
  • 4
  • 12