In another Bruce Eckels exercise in calculating velocity, v = s / t
where s and t are integers. How do I make it so the division cranks out a float?
Just cast one of the two operands to a float first.
v = (float)s / t;
The cast has higher precedence than the division, so happens before the division.
The other operand will be effectively automatically cast to a float by the compiler because the rules say that if either operand is of floating point type then the operation will be a floating point operation, even if the other operand is integral. Java Language Specification, §4.2.4 and §15.17