Jump to content

A question about the pre/post increment operator.....


Recommended Posts

Posted

In java, the order of pre/post increment operator comes first

 

but why, for eg,

 

int x = y++

 

the asignment operator executes first????, then y plus 1????

 

in my sense of logic, x should be y+1, not y

 

according to the order of operator in java, y++ and ++y should be no difference, because they are the first in the order

 

but actually, the JVM works not as what I expect, I think maybe the order of operator for java in my reference book is wrong, since

 

x = y+1, that '+' should come before post increment operator in order as the result shows

 

Can any one tell me how does the operator work in my particular case???

 

thx

 

Albert

 

Can any

Posted
In java' date=' the order of pre/post increment operator comes first

[/quote']

You misunderstand precedence in Java. The initial ++ is the pre increment operator. It has the highest precedence of all operators. The finall ++ is the post increment operator. It has the lowest precedence of all operators. The equal sign has the lowest precedence of all operators, with the exception of the post increment/decrement operators.

 

x = y ++

x = ++ y

 

Each of these has two operators. The highest preceddence is the initial ++. Next is the equal sign. Lowest is the final ++.

 

Therefore, in the first example the assignment will be first and in the second example the assignment will be second.

 

This simple beauty gives great power to these operators. The way you understood these operators strips them of half of their elegance.

Posted

the operators allow you to do two things at once, which makes typing easier, but when it's compiled it'll be two (or more) operations anyway. I say give your readers a break, type it on 2 lines for clarity!!

 

What, no one will ever read your code? hehe, until you try and read it yourself next week and can't figure out what you were trying to do....

Posted

Oh, thx you guys so much

 

Now I realise that even the reference book can be wrong :P

 

This what my reference states:

operator.JPG

 

 

This simple beauty gives great power to these operators. The way you understood these operators strips them of half of their elegance.

 

By the way, so what is the idea of the full elegance those operators in full play?? :)

 

thx for the responds

 

Albert

Posted
By the way, so what is the idea of the full elegance those operators in full play?? :)

Kygron, in the previous post, has a valid point. However, I do not agree with it. Programming has become an art form. The pre and post increment/decrement operators, together with the rest of the operators, enable programmers to create extremely compact code that is (fairly) simple to read, looks like art, and functions as well as if not better than much more verbose and ugly code. If you are an artist, learn to love your tools, the operators. If you are not, then do whatever you want. Its all the same to me, unless I were to consider you as a potential employee.

Posted

how about control structure????

 

"Algorithm + Data structure" plays the elegance of programming, right???

 

By the way, what's so special about data structure???

 

Albert

Posted
"Algorithm + Data structure"

Rather than answer in terms of what these words mean to me, and basically guess what you mean by these words, I would rather ask you to clarify your question.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.