int result = 5;
result = result--;
System.out.println(result);
Why is the result equal to 5?
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.
This does nothing :
Because
result--returns the value of result before it is decremented (contrary to--resultwhich 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
But if you want to decrement result, simply do
(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)