Home » Core Java » Java Division Example

About Aashu Aggarwal

Aashu has graduated in Computer Science from Kurukshetra University. During her career she has been mostly involved with Java and related frameworks like Spring, Spark along with Python, NodeJs. She has been developing and architecting on projects for fin-tech companies, retail, telecommunications to name a few. Currently she is working as Chief Agile Officer as well as Delivery head for a startup in India where she is setting up company wide processes for projects and working on projects based on Java, Python, Big Data and Machine Learning.

Java Division Example

In this article, we will have a look at integer java division example to see different ways of how division works.

1. How to perform division in Java

In java, / is the division operator. Depending upon the type of variables fed in the division operator, result of the division can be an integer or a value with precision.

1.1 Integer Division

When both of the variables are of int type or the denominator in the division is int, java performs integer division. Therefore, integer division is the same as a real division but the fraction part of the result is thrown away.

Let’s look at it with an example,

Integer Division Method
private int integerDivision(int a, int b){
        if(b == 0)
            return 0;
        else
            return a/b;
    }

Calling above method with variable a as 5, b as 3 will result in:

Invocation
public class JavaDivisionExample {

    public static void main(String[] args) {

        JavaDivisionExample javaDivisionExample = new JavaDivisionExample();

        int intDiv = javaDivisionExample.integerDivision(5, 3);
        System.out.println("Int Division: "+intDiv);

    }
Output
Int Division: 1

1.2 Precision Division

In some cases, we may need the complete result of the division along with its fraction part. In such scenarios, either both of the variables used for division must be double or float type or at least the denominator must be double or float data type.

Here, as we will see below, the result will consist of a fraction as well. Please note that you should collect the result of the division in a variable of the same data type as the data type of denominator. Without explicit downcasting, trying to store the result in a different type will give a compilation error.Precision Division Method

private int integerDivision(int a, int b){
        if(b == 0)
            return 0;
        else
            return a/b;
    }

We will be passing the same parameters as used above i.e. variable a as 5, b as 3, to see the difference in result.Invocation

public class JavaDivisionExample {

    public static void main(String[] args) {

        JavaDivisionExample javaDivisionExample = new JavaDivisionExample();
        double preDiv = javaDivisionExample.precisionDivision(5, 3);
        System.out.println("Precision Division: "+preDiv);
}
Output
Precision Division: 1.6666666666666667

1.3 Remainder

To calculate the remainder, java provides % operator. This operator returns the remainder of the division, if any, otherwise returns 0 if the numerator is a whole multiple of the denominator.Remainder Method

private int getRemainder(int a, int b){
        if(b == 0)
            return 0;
        else
            return a%b;
    }

Here, we will use the same parameters again i.e. variable a as 5 and b as 3 to see the result of the method.Invocation

public class JavaDivisionExample {

    public static void main(String[] args) {

        JavaDivisionExample javaDivisionExample = new JavaDivisionExample();
        int remainder = javaDivisionExample.getRemainder(5, 3);
        System.out.println("Remainder: "+remainder);

    }
Output
Remainder: 2

So, the above examples explain how division works in java both with and without precision data types. Also, we saw how can we compute the remainder.

2. Download the Source Code

Download
You can download the full source code of this example here: Java Division Example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments