In Java, there are two types of division—integer division and floating-point division (also known as remainder division). Both operations use the forward slash (/) as the operator in the formula dividend / divisor. Today, Mytour will guide you through how to divide two integers (without decimals) to obtain an integer quotient, as well as how to perform floating-point division to get a decimal result.
Steps
Integer Division

When dividing two integers in Java, the decimal portion (or remainder) will be discarded. For example, if you divide 7 by 3 on paper, you get 2 with a remainder of 1. However, when you divide two integers in Java, the remainder is discarded, and the result will only be 2. To perform integer division, use the following syntax:
int a = 7; int b = 3; int result = a / b; // result will be 2
- Integer division always returns an integer result. If you want a decimal result when dividing two integers, you should perform floating-point division.
- If you attempt to divide an integer by 0, an ArithmeticException will be thrown, even if the code compiles correctly.
Floating-Point Division

If the operands in the equation are of type float or double, you need to perform floating-point division. You can also use this type of division when you want to obtain the remainder of a division. To perform this division, both the dividend and divisor should be written using float syntax. For example, dividing 7 by 3 can be coded as follows:
float a = 7.0f; float b = 3.0f; int result = a / b; // result will be 2.33
- When using floating-point division by zero, the result returned will be NaN (Not a Number).
Advice
- When performing mixed division of integers and floating-point numbers, the floating-point values (float or double) will automatically be converted to double during division.