Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does *= not give any errors when implicitly casting a float to an int? [duplicate]

I ran into a situation earlier where I tried the following two bits of code:

int score = 100;
score = score * 1.05;

and

int score = 100;
score *= 1.05;

The first one failed (and obviously so, I'm trying to implicitly cast a float to an int). But the second one worked perfectly fine. The compiler didn't complain and I didn't get any runtime errors. Why does the second one work, while the first one doesn't? As far as I'm aware, x *= y is just a shorthand for x = x * y.

like image 677
Simon Verbeke Avatar asked Nov 12 '13 22:11

Simon Verbeke


Video Answer


2 Answers

Compound assignment operators behave a little differently than their "expanded" version. Quoting the JLS, Section 15.26.2:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

It is implicitly casted back to the type of the variable on the left side, so that there is no error casting a float to an int; it's already implicitly casted to an int.

That does not occur with the = operator, which is governed by the JLS, Section 5.2, Assignment Conversion:

Assignment contexts allow the use of one of the following:

  • an identity conversion (§5.1.1)

  • a widening primitive conversion (§5.1.2)

  • a widening reference conversion (§5.1.5)

  • a boxing conversion (§5.1.7) optionally followed by a widening reference conversion

  • an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

It goes on to talk about a possible narrowing conversion being allowed, but only for constant expressions, and only for the constant expression being a byte, char, short, or int, neither of which is applicable here.

like image 58
rgettman Avatar answered Nov 15 '22 16:11

rgettman


The first one

int score = 100;
score = score * 1.05;

is basically saying:

int score = 100;
score = (float)(score * 1.05);

This is because if you times a float with an integer then you get a float. This then cannot get assigned to an integer.

However

int score = 100;
score *= 1.05;

Basically means

int score = 100;
score = (int)(score * 1.05);

This is because you are not assigning the float, the calculation is done whilst assigning therefore first converts to an int.

Thats how it makes sense to me. Hope it helps

like image 45
Mitch Dart Avatar answered Nov 15 '22 16:11

Mitch Dart