Integer Division in Java

The article introduces a detailed explanation of what internally happens when we divide two integers.

In the below Java code, there are four java statements. Each line prints different output based on the numbers that divide from each other.

package integer_division;

public class IntegerDivision {
    public static void main(String[] args) {
        System.out.println(10 / 9);
        System.out.println(-10 / 9);
        System.out.println(10 / -9);
        System.out.println(10 / 19);
        System.out.println((float) 10 / 9);
        System.out.println((double) 10 / 9);
    }
}
  1. In case 1, a larger integer is divided with a smaller integer. The resultant output is integer 1. Mathematically, when we divide ten by nine, this results in a repeating number that is 1.1111.... But in the Java language, when an integer divides another integer, it throws away the remainder and keeps the quotient. Hence, the resulting output is an integer.
  2. In case 2, when we divide the negative integer to a positive integer, this results in an integer as -1. As stated above, the integer throws away the remainder and keeps the quotient. Hence the resulting value is a negative integer.
  3. In case 3, there is the reverse case where the numerator is a positive integer, and the denominator is a negative integer. The resultant output is -1. Again, the negative sign in the numerator or denominator does not matter much. It simply divides the two numbers without keeping any remainder. And hence the output is again a negate of integer.
  4. In case 4, the denominator is larger than the numerator, which results in a number small than 1. As stated above, the number truncates after zero and prints the value 0 as its output.
  5. In case 5, we are manually typecasting the output to a float value.

Let’s understand typecasting first.

Typecasting is the process of converting one data type to another, and we can either do this manually or automatically.

  1. Widening Typecasting: It is often called implicit conversion. In this type of casting, a smaller datatype converts to a higher data type. The compiler does this casting automatically.
  2. Narrowing Typecasting: It is often called explicit conversion. In this type of casting, a larger datatype is cast to some smaller data type, resulting in data loss.

So, in case 3, the data implicitly truncates by the compiler is manually cast to float number, which results in a complete rational number. This number displays data up to 8 digits.

Similarly, in case 6, we will manually caste the integer division to a double value. So this will result in a decimal number that displays numbers up to 16 digits.

Refer below section for the output of the above code.

1
-1
-1
0
1.1111112
1.1111111111111112
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Integer

  • Check if Input Is Integer in Java