Integer division Java

Published in the Java Developer group

What is integer division in Java?

Division in Java takes place normally like regular division in mathematics or real life. However, it just discards the remainder. For example, if you divide 9 by 2 the quotient is 4 and the remainder is 1. Integer division Java - 1In real life, the answer is 4.5 or 4½. If you perform the same calculation with int in Java, your answer will be 4. It is not rounded to the closest integer (like ~4.5 = 5) 1 as a remainder is thrown away.

Example 1 [ Remainder is 0 ]

The integer division in Java works perfectly for all the cases where the divisor completely divides the dividend (integer divided by x integer). The answer is a whole number and the integer data type can hold it without overflow. Hence there is no loss of data. For example, have a look at the following snippet.

public class IntegerDivision {

	public static void main(String[] args) {
		
		int dividend = 100;
		int divisor = 5;
		int quotient = dividend / divisor;
		
		//Dividend completely divides the divisor
		System.out.println(dividend + " / " + divisor + " = " + quotient);
			
		dividend = 143;
		divisor = 11;
		quotient = dividend / divisor;
		
		//Dividend completely divides the divisor
		System.out.println(dividend + " / " + divisor + " = " + quotient);
	}
}
Output
100 / 5 = 20 143 / 11 = 13

Example 2 [ Remainder is not 0 ]

For all the division cases where the remainder is not 0, the final result will be chopped off to the largest divisible integer (9/2 = 4). This will be exhibited in the upcoming example. There might be times when you need the actual quotient in decimal. For that case, you can use the float or double data type. However, if you wish to round off the quotient to the closest int you can do the following.

public class IntegerDivision {

	public static void main(String[] args) {

		int dividend = 9;
		int divisor = 2;
		int quotient = dividend / divisor;
		
		// Case I - Dividend does not divide the divisor completely
		// The quotient is chopped / truncated
		System.out.print("Integer division \t\t" );
		System.out.println(dividend + " / " + divisor + " = " + quotient);
		
		// Case II - Mathematical or real life division
		// Use float or double data type to get the actual quotient 
		
		double actualQuotient = (double)dividend / divisor;
		System.out.print("Mathematics division \t\t" );
		System.out.println((double)dividend + " / " + divisor + " = " + actualQuotient);
		
		// Case III - Integer Division with rounding off 
		// the quotient to the closest integer
		
		long roundedQuotient = Math.round((double)dividend / divisor);
		System.out.print("Round off int division \t\t" );
		System.out.println((double)dividend + " / " + divisor + " = " + roundedQuotient);

	}
}
Output
Integer division 9 / 2 = 4 Mathematics division 9.0 / 2 = 4.5 Round off int division 9.0 / 2 = 5

Explanation

Case I and Case II are self explanatory. For Case III, you can break it down in the following steps.
  • First, you need to convert the dividend to a double.

  • Perform the regular Java int division.

  • Round off the quotient using the Math.round() method.

  • Use long datatype to store the rounded quotient.

  • There you go! You have your desired output as the quotient.

Conclusion

Division with Java integers can look tricky in the beginning. But with some practice and repetition, you can get a hold of it. Do as much practice as you can. Feel free to get back to our post any time you feel like. Cheers! Integer division Java - 2
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION