The 123
literal is int
by default, not double. There is no problem to assign an int
literal to a float
, since that requires a widening primitive conversion.
When you assign the value of an expression to a variable, the following assignments are allowed :
JLS 5.2. Assignment contexts :
Assignment contexts allow the value of an expression to be assigned (§15.26) to a variable; the type of the expression must be converted to the type of the variable.
Assignment contexts allow the use of one of the following:
JLS 5.1.2. Widening primitive Conversion
19 specific conversions on primitive types are called the widening
primitive conversions:
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
...