Duplicate:
When i
is an integer and d
is a double
, i+=d
works but i= i+d
does not.
Why is this?
When i
is an integer and d
is a double
, i+=d
works but i= i+d
does not.
Why is this?
i = i + d does not work because you would be assigning a double to an int and that is not allowed.
The += operator casts the double automatically to an int, so that is why it works.
This is the link to the information on the spec: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#5304