Can you please help me understand what the following code means:
x += 0.1;
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.
The “common knowledge” of programming is that
x += yis an equivalent shorthand notation ofx = x + y. As long asxandyare of the same type (for example, both areints), you may consider the two statements equivalent.However, in Java,
x += yis not identical tox = x + yin general.If
xandyare of different types, the behavior of the two statements differs due to the rules of the language. For example, let’s havex == 0(int) andy == 1.1(double):+=performs an implicit cast, whereas for+you need to explicitly cast the second operand, otherwise you’d get a compiler error.Quote from Joshua Bloch’s Java Puzzlers: