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 A simple calculation of --     ((-y--) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A simple calculation of --     ((-y--)" Watch "A simple calculation of --     ((-y--)" New topic
Author

A simple calculation of -- ((-y--)

jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43


i wanna ask why x=1? and y= -2?
thanks a lot!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19787
    
  20

It's all a matter of operator precedence, and the way the postfix decrement operator works. y-- decreases y but returns the old value of y. So y-- returns -1, then decreases y to -2. That -1 is then negated to 1.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Rob Spoor wrote:It's all a matter of operator precedence, and the way the postfix decrement operator works. y-- decreases y but returns the old value of y. So y-- returns -1, then decreases y to -2. That -1 is then negated to 1.

i am sorry but why it is not do the -- first y=y-1 then y=-2 and times the - , become 2
thanks...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

because all operators have a precedence. Just like in the equation "2 + 3 * 4", it is understood that multiplication comes before division, it is understood that the negation operator will be applied before the post-fix decrement operator.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Actually, it is not a matter of precedence, but as Rob has already explained, it's simply how the postfix-decrement works. It decrements the variable, and returns the old value. If that's not what you want, then don't use it.
jack parker
Ranch Hand

Joined: Oct 18, 2011
Posts: 43
Stephan van Hulst wrote:Actually, it is not a matter of precedence, but as Rob has already explained, it's simply how the postfix-decrement works. It decrements the variable, and returns the old value. If that's not what you want, then don't use it.

i got so confused here, then when will it decrease 1 ? it always returns the old value? is that mean the -- is ignored in x?
and how y gets -2
thanks a lot!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

jack parker wrote:i got so confused here, then when will it decrease 1 ? it always returns the old value? is that mean the -- is ignored in x?
and how y gets -2

Are you sure the code you've posted is the correct code? Because there's no -- with x. In fact, there's no x variable declared or used anywhere.

y gets to -2 simply because you set it to -1, and then decrease it by one.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776

read like -(y--). The effect of the post fix operator is felt once the statement that has the post fix operator is executed. So in this line though postfix operation is applied, the effect do not take place.

So y is still -1 in this statement and a negation makes the value of 1 to be printed near x.

There is a semicolon at the last that ends the statement. Now the effect is seen and y changes to -2.


Next statement prints y's value that's -2.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

jack parker wrote:i got so confused here, then when will it decrease 1 ? it always returns the old value? is that mean the -- is ignored in x?
and how y gets -2
thanks a lot!


There is a difference between the variable and the expression. For example....

y = x--;

The variable x does get decremented, however, the "x--" expression returns the old value, and hence, the variable y is assigned the old variable.

Having the expression be the old value doesn't mean that the variable isn't decremented.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple calculation of -- ((-y--)