albertlee Posted February 20, 2005 Posted February 20, 2005 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
Sayonara Posted February 21, 2005 Posted February 21, 2005 ++y will increment the value, then use it. y++ will use the value, then increment it. Or is it the other way around?
Cadmus Posted February 21, 2005 Posted February 21, 2005 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.
Cap'n Refsmmat Posted February 21, 2005 Posted February 21, 2005 An example y = 1 x = 0 x = y++ //x now equals 1, because y was incremented after it was assigned to x x = ++y //x is three, because y is incremented before.
Kygron Posted February 21, 2005 Posted February 21, 2005 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....
albertlee Posted February 21, 2005 Author Posted February 21, 2005 Oh, thx you guys so much Now I realise that even the reference book can be wrong This what my reference states: 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
Cadmus Posted February 22, 2005 Posted February 22, 2005 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.
Silencer Posted February 22, 2005 Posted February 22, 2005 If programming is an art, then Perl mongers are both Picasso and Michelangelo in one.
albertlee Posted February 22, 2005 Author Posted February 22, 2005 how about control structure???? "Algorithm + Data structure" plays the elegance of programming, right??? By the way, what's so special about data structure??? Albert
Cadmus Posted February 22, 2005 Posted February 22, 2005 "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.
albertlee Posted February 22, 2005 Author Posted February 22, 2005 Just want to say, I feel "Algorithm + Data Structure" is the idea of art in porgramming Albert
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now