Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

Authors Top

If you have a few years of experience in the Java ecosystem, and you’d like to share that with the community, have a look at our Contribution Guidelines.

1. Overview

We can use the division operator (/) to divide the left-hand value by the right-hand one in Java. For instance, int number = 10 / 5.

In this quick tutorial, we'll explore how to get a float result from the integer division operation.

2. Introduction to the Problem

First of all, for simplicity, we'll use unit test assertions to verify the calculation result in this tutorial.

We may have realized when we apply the division operator on two integers like a/b, it always returns an integer, even though a isn't evenly divisible by b, for example:

int i = 10 / 4;
assertEquals(2, i);

If we run the test, it passes. So, 10 / 4 produces 2 instead of 2.5. Moreover, even if we assign the calculation result to a float variable, the result is still two:

float x = 10 / 4;
assertEquals(2, x);

So next, let's first understand why this happens and then figure out how to get the desired float result, for example, 2.5 in this case.

3. Why Does the Division of Two Integers Always Result in an Integer in Java?

To understand why 10 / 4 = 2 in Java, we need to check the division operator section in JLS (Java Language Specification) to know how the division operator's behavior is defined.

First, the specification states that integer division rounds toward zero. In other words, the result of the division operation of two integers is only the quotient value. The remainder is not included.

Therefore, when we calculate 10 / 4, we get 2 instead of 2.5.

Next, let's see how to get the expected float result if the two integers are not divisible.

4. How to Make Division of Two Integers Result in a Float?

Now, we've understood how the division operator works with two integers. JLS has also defined primitive operands conversion rules in Binary numeric promotion.

Binary numeric promotion is performed on *, /, and % operators. Therefore, the division operation follows the operand conversion rules.

So next, let's take a closer look at the operand conversion rule.

As per the JLS, when we calculate a/b:

  • If either operand is of type double, the other is converted to double.
  • Otherwise, if either operand is of type float, the other is converted to float.
  • Otherwise, if either operand is of type long, the other is converted to long.
  • Otherwise, both operands are converted to type int.

Based on the rule above, if we want to make the a/b operation result in a float number, at least one operand should be of type float. 

So, next, let's see if we can get the expected result when we cast one operand to float:

float x = (float) 10 / 4;
assertEquals(2.5, x);

float y = 10 / (float) 8;
assertEquals(1.25, y);

The test above passes if we give it a run. Therefore, casting any operand to float makes the division produce a float result.

5. Conclusion

In this quick article, we've discussed why the division of integers always results in integers in Java.

Further, we've addressed how to get the expected float result from the division of integers. This is particularly useful for nondivisible cases.

As usual, all code snippets presented in the article are available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments