LogIn
I don't have account.

Divide by Zero in Java

DevSniper

141 Views
Division by zero is a fundamental mathematical operation that is undefined. In this article we will learn and understand with example how to divide any number by zero results in specific behaviors depending on the data type. Let's explore how division by zero is handled in Java across different data types and scenarios.
Example 1 : Integer Division (int, long, etc.)
Whenever we perform zero division with integer in java results is an ArithmeticException being thrown at runtime.
Copy
public class Program 
{
    public static void main(String []args)
    {
        System.out.println("10/0 =  "+ 10/0);
        //OR
        System.out.println("10L/0 =  "+ 10L/0);
    }
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Program.main
Handling Division by Zero
Copy
public class Program 
{
    public static void main(String []args) 
    {
        int numerator =10;
        int denominator =0;
        try {
            int result = numerator / denominator;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            // Handle the exception or log it as per your application's needs
            System.err.println("Division by zero is not allowed.");

        }
    }
}
Division by zero is not allowed.
Preventing Division by Zero
Copy
public class Program 
{
    public static void main(String []args) 
    {
        int numerator =10;
        int denominator =0;
        if(denominator != 0) {
            int result = numerator / denominator;
            System.out.println("Result: " + result);
        } else {
            // Handle the exception or log it as per your application's needs
            System.err.println("Division by zero is not allowed.");
        }
    }
}
Division by zero is not allowed.
Example 2 : floating-point numbers (float and double)
Whenever we are trying to perform zero division with float and double in Java, results in special floating-point values rather than causing a runtime exception like ArithmeticException for integers.it depends on the numeric type (float or double) and the direction of the division (positive or negative).
Positive Infinity :-
Copy
public class Program
{
    public static void main(String []args)
    {
        
        System.out.println("10/0.0 =  "+ (10/0.0) );
        System.out.println("10L/0.0 =  "+ (10L/0.0) );
        System.out.println("10f/0.0 =  "+ (10/0.0) );
        System.out.println("10f/0 =  "+ (10f/0) );
        System.out.println("10f/-0 =  "+ (10f/-0) );
        System.out.println("-10/-0f =  "+ (-10/-0f) );
        System.out.println("10.1/0.0 =  "+ (10.1/0.0) );
        System.out.println("10.1/0 =  "+ (10.1/0) );
        System.out.println("10.1/-0 =  "+ (10.1/-0) );
        System.out.println("-10.1/-0.0 =  "+ (-10/-0.0) );
    }
}
10/0.0 =  Infinity
10L/0.0 =  Infinity
10f/0.0 =  Infinity
10f/0 =  Infinity
10f/-0 =  Infinity
-10/-0f =  Infinity
10.1/0.0 =  Infinity
10.1/0 =  Infinity
10.1/-0 =  Infinity
-10.1/-0.0 =  Infinity
Negative Infinity :-
Copy
public class Program 
{
    public static void main(String []args) 
    {
        System.out.println("10/-0.0 =  "+ (10/-0.0) );
        System.out.println("10L/-0.0 =  "+ (10L/-0.0) );
        System.out.println("10f/-0.0 =  "+ (10f/-0.0) );
        System.out.println("-10f/0.0 =  "+ (-10f/0.0) );
        System.out.println("-10f/0 =  "+ (-10f/0) );
        //System.out.println("10f/-0 =  "+ (10f/-0) ); +Infinity
        System.out.println("10.1/-0.0 =  "+ (10.1/-0.0) );
        System.out.println("-10.1/0.0 =  "+ (-10.1/0.0) );
        System.out.println("-10.1/0 =  "+ (-10.1/0) );
        //System.out.println("10.1/-0 =  "+ (10.1/-0) ); +Infinity
    }
}
10/-0.0 =  -Infinity
10L/-0.0 =  -Infinity
10f/-0.0 =  -Infinity
-10f/0.0 =  -Infinity
-10f/0 =  -Infinity
10.1/-0.0 =  -Infinity
-10.1/0.0 =  -Infinity
-10.1/0 =  -Infinity
NaN (Not a Number)
Copy
public class Program 
{
    public static void main(String []args) 
    {
        System.out.println("0/0.0 =  "+ (0/0.0) );
        System.out.println("0/-0.0 =  "+ (0/-0.0) );
        System.out.println("-0/0.0 =  "+ (-0/0.0) );
        System.out.println("-0/-0.0 =  "+ (-0/-0.0) );
        System.out.println("0/0f =  "+ (0/0f) );
        System.out.println("0/-0f =  "+ (0/-0f) );
        System.out.println("-0/0f =  "+ (-0/0f) );
        System.out.println("-0/-0f =  "+ (-0/-0f) );
    }
}
0/0.0 =  NaN
0/-0.0 =  NaN
-0/0.0 =  NaN
-0/-0.0 =  NaN
0/f =  NaN
0/-0f =  NaN
-0/0f =  NaN
-0/-0f =  NaN
Zero With Negative Sign
Copy
public class Program
{
    public static void main(String []args)
    {
        System.out.println("-0 =  "+ (-0) );
        System.out.println("-0L =  "+ (-0L) );
        System.out.println("-0f =  "+ (-0f) );
        System.out.println("-0.0 =  "+ (-0.0) );
    }
}
-0 =  0
-0L =  0
-0f =  -0.0
-0.0 =  -0.0