How to assign Infinity in Java

In this post, I will be sharing how to implement infinity in Java. We will implement double positive infinity, double negative infinity, float positive infinity, float negative infinity, operations with infinity,  and use division with zero.

Read Also: Difference between / and % in Java

Let's dive deep into the topic:

How to Assign Infinity in Java

We have in-built constant variables in Java that hold the infinity value. For example, the Double class contains POSITIVE_INFINITY and NEGATIVE_INFINITY constant variables holding the positive and negative infinity of type double as shown below.

 public static final double POSITIVE_INFINITY
public static final double NEGATIVE_INFINITY


Similarly, the Float class contains POSITIVE_INFINITY and NEGATIVE_INFINITY constant variables holding the positive and negative infinity of type float.

 public static final float POSITIVE_INFINITY
public static final float NEGATIVE_INFINITY


Double and Float classes are available in java.lang package, a default package that is automatically imported in the Java program.

Positive Infinity in Java



1. Using Double
2. Using Float

1. Using Double



According to Oracle docs, we can implement the positive infinity in Java using Double class as shown below in the example:

 public class InfinityJava {
    public static void main(String args[]) {
      double positiveInf = Double.POSITIVE_INFINITY;
      System.out.println(positiveInf);
    }
}


Output:
Infinity


2. Using Float



According to Oracle docs, we can implement the positive infinity in Java using Float class as shown below in the example:

 public class InfinityJava2 {
    public static void main(String args[]) {
      float positiveInf = Float.POSITIVE_INFINITY;
      System.out.println(positiveInf);
    }
}


Output:
Infinity


Negative Infinity in Java



1. Using Float
2. Using Double

1. Using Double



We can implement the negative infinity in Java using Double class as shown below in the example:

 public class InfinityJava3 {
    public static void main(String args[]) {
      double negativeInf = Double.NEGATIVE_INFINITY;
      System.out.println(negativeInf);
    }
}


Output:
-Infinity


2. Using Float

We can implement the negative infinity in Java using the Float class in Java as shown below in the example:

 public class InfinityJava4 {
    public static void main(String args[]) {
      float negativeInf = Float.NEGATIVE_INFINITY;
      System.out.println(negativeInf);
    }
}


Output:
-Infinity


Operations with Infinity



Below are some of the operations we can perform with infinity:

 public class InfinityJava5 {
    public static void main(String args[]) {
      double positiveInf = Double.POSITIVE_INFINITY;    
      double negativeInf = Double.NEGATIVE_INFINITY;
      System.out.println(positiveInf - negativeInf);
      System.out.println(negativeInf - positiveInf);
      System.out.println(positiveInf + negativeInf);
    }
}


Output:
Infinity
-Infinity
NaN


Use division with zero



To implement infinity in Java, we can simply divide the number by zero as shown below in the example.

 public class InfinityJava6 {
    public static void main(String args[]) {
      double num = 100.0/0.0;
      System.out.println(num);
      double num2 = -10.0/0.0;
      System.out.println(num2);
    }
}


Output:
Infinity
-Infinity


That's all for today, please mention in the comments in case you have any questions related to how to assign infinity in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry