8

I have the following code:

float f = 1.5f;
int i = 3;
i *= f;

I receive no warning, but if I do

i = i*f

I receive an incompatible type warning. Why does Java automatically cast the first multiplication but not the second? What is the reasoning behind this?

Daiwik Daarun
  • 3,804
  • 7
  • 33
  • 60
  • Good answer, couldn't find it originally though because of poor keyword searches. Thanks :) – Daiwik Daarun May 02 '15 at 19:46
  • 1
    Just for all the people that are as curios as I was: This will fail in C# for both assignments with the following error: `Error 1 Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)` – Markus May 02 '15 at 19:46

0 Answers0