I'm doing multiplication and division of float
s and int
s and I forget the implicit conversion rules (and the words in the question seem too vague to google more quickly than asking here).
If I have two int
s, but I want to do floating-point division, do I need only to cast one or both of the operands? How about for multiplication — if I multiply a float
and an int
, is the answer a float
?
Any integral numeric type is implicitly convertible to any floating-point numeric type. There are no implicit conversions to the byte and sbyte types. There are no implicit conversions from the double and decimal types. There are no implicit conversions between the decimal type and the float or double types.
So, when you assign an int value to float variable, the conversion of int to float automatically happens in Java. This is also called widening casting or widening primitive conversion. So, to convert an in to float using widening casting, you can just assign a value of int to float.
An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.
Use Direct Assignment to Convert Float to Int The conversion between the float and int values can be done using the assignment operator. In this case, the float variable will be implicitly converted to the int type, and the value will be narrowed down to the second type, losing all digits after the decimal point.
You can’t assign to an int
result from division of a float
by an int
or vice-versa.
So the answers are:
If I have two
int
s, but I want to do floating point division…?
One cast is enough.
If I multiply a
float
and anint
, is the answer afloat
?
Yes it is.
float f = 1000f;
int i = 3;
f = i; // Ok
i = f; // Error
f = i/f; //Ok 0.003
f = f/i; //Ok 333.3333(3)
i = i/f; //Error
i = f/i; //Error
To demonstrate:
int i1 = 5;
float f = 0.5f;
int i2 = 2;
System.out.println(i1 * f);
System.out.println(i1 / i2);
System.out.println(((float) i1) / i2);
Result:
2.5
2
2.5
In order to perform any sort of floating-point arithmetic with integers, you need to convert (read: cast) at least one of the operands to a float
type.
If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other is integral.
(Source: Java language specifications - 4.2.4)
if I multiply a float and an int, is the answer a float?
System.out.println(((Object)(1f*1)).getClass());//class java.lang.Float
(If you use DrJava, you can simply type ((Object)(1f*1)).getClass() into the interactions panel. There's a plugin for DrJava for Eclipse too.)
The simple answer is that Java will perform widening conversions automatically but not narrowing conversions. So for example int->float is automatic but float->int requires a cast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With