0

Duplicate:

java += question

Why aren’t op-assign operators type safe in java?

When i is an integer and d is a double, i+=d works but i= i+d does not.

Why is this?

Community
  • 1
  • 1

1 Answers1

1

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

Mario Ortegón
  • 18,670
  • 17
  • 71
  • 81