My Java teacher said it was better to use ++n instead of n++, I am not seeing the logic behind this. Does anyone know?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
++nincrements the value and returns the new one.n++increments the value and returns the old one.Thus,
n++requires extra storage, as it has to keep track of the old value so it can return it after doing the increment.I would expect the actual difference between these two to be negligible these days. I know a lot of compilers will optimize it so they’re identical if the return of
n++isn’t actually used, though I don’t know of Java does that.