23

I'm doing multiplication and division of floats and ints 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 ints, 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?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Rich
  • 36,270
  • 31
  • 115
  • 154
  • BTW, you should be able to write a <10-line Java method that tests this for you. – Matt Ball Nov 04 '10 at 14:52
  • 4
    agreed, but I don't have a quick and dirty test harness for this because the only thing I ever do with Java is create Android projects in Eclipse so I wouldn't even know (without looking it up) how to create a simple cmd line app in Java. Quicker to ask here and get an answer in literally seconds while I continue to work. =) – Rich Nov 04 '10 at 15:09
  • 3
    But now everyone on earth doesn't have to test this. – Duncan Calvert Nov 07 '13 at 00:50

6 Answers6

35

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 ints, but I want to do floating point division…?

One cast is enough.

If I multiply a float and an int, is the answer a float?

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
Ry-
  • 218,210
  • 55
  • 464
  • 476
  • Why this statement correct? ```f = i/f; //Ok 0.003``` I would have expected to see 0. What is the rule when you divide float / int or int/float? Many thanks – bibscy Nov 13 '19 at 15:30
  • 1
    @bibscy You would have expected zero why? The presence of the `float` makes it a floating-point operation, as stated in this answer and several others. – user207421 Nov 27 '19 at 20:54
11

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
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
7

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.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
7

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.)

blizpasta
  • 2,624
  • 3
  • 25
  • 31
6

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Java ranks primitive types in the order int < long < float < double. If an operator is used with different primitive types, the type which appears before the other in the above list will be implicitly converted to the other, without any compiler diagnostic, even in cases where this would cause a loss of precision. For example, 16777217-1.0f will yield 16777215.0f (one less than the correct value). In many cases, operations between a float and an int outside the range +/-16777216 should be performed by casting the int to double, performing the operation, and then--if necessary--casting the result to float. I find the requirement for the double casting annoying, but the typecasting rules in Java require that one either use the annoying double cast or suffer the loss of precision.

supercat
  • 77,689
  • 9
  • 166
  • 211