# 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);
}
}
```

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

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

**Rashmi Patidar**

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn## Related Article - Java Integer

- Handle Integer Overflow and Underflow in Java
- Reverse an Integer in Java
- Convert Int to Binary in Java
- The Max Value of an Integer in Java
- Minimum and Maximum Value of Integer in Java
- Convert Integer to Int in Java