# Implementing Positive and Negative Infinity in Java

In this tutorial, we are learning how to implement infinity in Java. We will implement both positive and negative infinity here.

## Positive Infinity in Java

Let’s see how we can implement infinity. In java, we have inbuilt constant variables which store the infinity value and it isĀ available in java.lang package and it is a default package that is automatically imported in the java program.

double positive_inf=Double.POSITIVE_INFINITY; System.out.println(positive_inf);

In the above statements, we have defined a variable ‘positive_inf’ with the type ‘double’ and is initialized with ‘Double.POSITIVE_INFINITY’ which is an inbuilt java infinite variable. So the output looks like as below:

Infinity

## Implementing Negative Infinity in Java

Implementing negative infinity is similar to implementing positive infinity. In this also, we have an inbuilt constant variable that stores the negative infinite value.

double negative_inf=Double.NEGATIVE_INFINITY; System.out.println(negative_inf);

In the above statements, we have defined a variable ‘negative_inf’ with the type ‘double’ and is initialized with ‘Double.NEGATIVE_INFINITY’ which is an inbuilt java infinite variable. So the output looks like as below:

-Infinity

## Operations with these constant variables

Here I will be demonstrating the operations with these constant variables

We will be subtracting negative infinity from positive infinity and vice versa

System.out.println(positive_inf-negative_inf); System.out.println(negative_inf-positive_inf);

The output looks like

Infinity -Infinity

We will be adding negative infinity and positive infinity

System.out.println(positive_inf+negative_inf);

The output looks like

NaN

## Leave a Reply