5
int result = 5;    
result = result--;  
System.out.println(result);  

Why is the result equal to 5?

Termininja
  • 6,620
  • 12
  • 48
  • 49
ivan angelo
  • 157
  • 1
  • 8

8 Answers8

15

Because the value of the expression result-- is the value of result before the decrement. Then result is decremented and finally the value of the expression is assigned to result (overwriting the decrement).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
14

This does nothing :

   result = result--;  

Because result-- returns the value of result before it is decremented (contrary to --result which returns the value of result after it is decremented). And as the part to the right of = is executed before the =, you basically decrement result and then, on the same line, set result to the value it had before the decrement, thus canceling it in practice.

So you could write

   result = --result;  

But if you want to decrement result, simply do

result--; 

(or --result; but it's very unusual and atypical to use it on its own, don't do it when you don't want to use the result of the expression but simply want to decrement)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    "this is very unusual and atypical, don't do it" - this is pure gold :) – maksimov Jun 18 '12 at 16:17
  • 1
    @maksimov : I'm not a native english speaker so maybe I miss something, can you explain ? – Denys Séguret Jun 18 '12 at 16:18
  • What's wrong with prefix decrement? :) – jsalonen Jun 18 '12 at 16:19
  • @jsalonen its unusual and atypical, which it is to fair – NimChimpsky Jun 18 '12 at 16:19
  • I was referring to using it on his own, not in an expression using it. – Denys Séguret Jun 18 '12 at 16:19
  • @dystroy your English is fine, it's just the statement sounds as if we are dealing with something superficial here and we don't really know what to expect :) – maksimov Jun 18 '12 at 16:22
  • 3
    A few days before I was told on SO that I was making hard to read code by writing `for(var i=a.length;i-->0;)` ... – Denys Séguret Jun 18 '12 at 16:23
  • 2
    It is indeed quite hard to read – maksimov Jun 18 '12 at 16:24
  • I can understand this as a "this is just how it works" but I'd really like to understand it in more depth. Could someone explain this in more detail? From my current understanding, even though `result = result--;` is weird, it should still _work_ For example `result--;` will decrement result and the value of result will actually change. So if you do `result = result--;` wouldn't the logic work like this? `result = result;` is what it sees first. Then, wouldn't it perform the `result--;`? Why doesn't it? – Sephallia Jun 18 '12 at 17:07
  • this line gives to result the value of `result--`. `result--` does 2 things : it decrements AND it returns a value (the one before the decrement). So that just after having decremented (what's on the right of the `=` is executed first), you set the value of result to the one before the decrement. So you do a decrement and then cancel it. – Denys Séguret Jun 18 '12 at 17:28
5

Very valid question!

Your code doesn't work since result-- is performed only after the substitution =. Your code would work had you used prefix operator:

result = --result;

However that doesn't make any sense as you can simply write:

--result;

For a more thorough explanation see this question/answers on how prefix/postfix operators work on Java.

Community
  • 1
  • 1
jsalonen
  • 29,593
  • 15
  • 91
  • 109
4

This is confusing construct. Its the same as

int tmp = result;
result--;  
result = tmp;
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

You are using the post-decrement operator. This is because you are writing result-- but not --result. The value of the expression result-- is a copy of result, it is defined before the decrementing. That's why it's called post-decrement.

After the value of the expression result-- is defined, result is decremented. But: Right after this, the value of result is overriden by the value of the result-- expression (result = 5), which was 5.

theV0ID
  • 4,172
  • 9
  • 35
  • 56
2

result-- is post decrement.. which means that the value is first assigned to result and then decremented.

you might find this answer helpful.

I believe what you want to do is :

int result = 5;     
result--; 
System.out.println(result);  
Community
  • 1
  • 1
Osama Javed
  • 1,432
  • 1
  • 16
  • 21
0

change to prefix increment to get desired result, or remove the assignment statement:

int result = 5;
result = --result;
System.out.println(result);
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

try

result=--result; it will print 4, and you will come to the understanding of what is the diffrence between c-- and --c

Bassel Shawi
  • 604
  • 4
  • 11
  • 29