Integer Division in Java

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about integer division in Java. Division is the mathematical operation that allows us to divide one quantity by another. In Java, there are different ways to perform integer division, and it is important to understand the differences between them.

Setting up the Environment

Open the terminal and create a new Java file using the following command:

touch ~/project/IntegerDivision.java

Simple Integer Division

In this step, you will learn how to perform simple integer division in Java. Simple integer division is when both the divisor and dividend are integer numbers and the result is also an integer.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 25;
        int b = 5;
        int c = a / b;
        System.out.println(a + " / " + b + " = " + c);
    }
}

Here, we define two integers a and b, and we perform the division operation by using the / operator. The result is stored in the integer c. Finally, we print the result using System.out.println().

To run the code, use the following command:

javac IntegerDivision.java && java IntegerDivision

The output should be:

25 / 5 = 5

Losing Data in Integer Division

In this step, you will learn how to handle situations where you can lose precision in integer division.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        int c = a / b;
        System.out.println(a + " / " + b + " = " + c);
    }
}

In this case, we define two integers a and b, but we get a different result because we lose precision. The result of this operation is not 3.3333 but rather 3, since we are dealing with integers.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

10 / 3 = 3

Division with Double Data Type

In this step, you will learn how to handle situations where you need to maintain precision.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        double c = (double) a / b;
        System.out.println(a + " / " + b + " = " + c);
    }
}

Here, we define two integers a and b. However, we change the data type of c from int to double. We also cast a to a double to ensure that the division operation is done using double precision. Finally, we print the result.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

10 / 3 = 3.3333333333333335

Division with Common Double Data Type

In this step, you will learn another way of handling situations where you need to maintain precision.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        double c = a / 3.0;
        System.out.println(a + " / 3.0 = " + c);
    }
}

Here, we simply change one of the operands to a double by using 3.0 instead of 3. This ensures that a is divided using double precision.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

10 / 3.0 = 3.3333333333333335

Multiple Divisions with Common Double Data Type

In this step, you will learn how to perform multiple divisions with double precision.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        double c = a / 3.0;
        double d = 2.0 / b;
        System.out.println(a + " / 3.0 = " + c);
        System.out.println("2.0 / " + b + " = " + d);
    }
}

We define two double variables c and d, and we perform two divisions. As mentioned before, we use 3.0 to ensure double precision in the first division. In the second division, we use 2.0 to ensure double precision.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

10 / 3.0 = 3.3333333333333335
2.0 / 3 = 0.6666666666666666

Division Assignment

In this step, you will learn how to perform a division using the division assignment operator /=, which is a shorthand notation for x = x / y.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        double c = 2.0;
        c /= b;
        System.out.println("2.0 / " + b + " = " + c);
    }
}

We define an int variable a, an int variable b, and a double variable c. We initialize c to 2.0. Then we use the division assignment operator to divide c by b. Finally, we print the result.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

2.0 / 3 = 0.6666666666666666

Integer Division with Modulo Operator

In this step, you will learn about the modulo operator %, which gives you the remainder of a division operation.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        int c = a % b;
        System.out.println(a + " % " + b + " = " + c);
    }
}

We define int variables a and b, and we use the modulo operator % to get the remainder of the division operation. The result is stored in the int variable c, and we print the result.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

10 % 3 = 1

Division by zero

In this step, you will learn what happens when you divide by zero.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 0;
        int c = a / b;
        System.out.println(a + " / " + b + " = " + c);
    }
}

We define two int variables a and b, and we try to divide a by b. Since dividing by zero is not allowed, Java throws a runtime exception called ArithmeticException.

If you try to run the code, you will see an error message like this:

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at IntegerDivision.main(IntegerDivision.java:5)

Fixing Division by zero

In this step, you will learn how to fix division-by-zero errors using conditional statements.

public class IntegerDivision {
    public static void main(String[] args) {
        int a = 10;
        int b = 0;
        if (b != 0) {
            int c = a / b;
            System.out.println(a + " / " + b + " = " + c);
        } else {
            System.out.println("Cannot divide by zero");
        }
    }
}

Here, we use an if statement to check whether b is not equal to zero. If b is not zero, we perform the division operation and print the result. Otherwise, we print an error message.

To run the code, use the same command as in step 2:

javac IntegerDivision.java && java IntegerDivision

The output should be:

Cannot divide by zero

Summary

In this lab, you learned about different ways of performing integer division in Java. You learned how to handle situations where you may lose precision, how to perform divisions with double precision, and how to use the modulo operator. You also learned how to handle division-by-zero errors using conditional statements.

Other Java Tutorials you may like