tags:

views:

207

answers:

1

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?

+1  A: 

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