Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll have a look at the concept of infinity in Java and how we can work with it.

2. Intro to Numbers in Java

In mathematics, we have a set of real numbers and a set of integers. Evidently, both of these sets are unlimited, and both contain positive and negative infinity.

In the computer world, we need a memory location where we can store the values for these sets, and this location must be finite as the memory of a computer is finite.

For int type in Java, the concept of infinity is not covered. We can only store integer numbers that fit in the memory location that we chose.

For real numbers, we also have the concept of infinity, either positive or negative. The 32 bits float type and also the 64 bits double type supports this in Java. Moving forward, we'll use the double type for exemplifying as it is also the most used type for real numbers in Java, for it has better precision.

3. Positive Infinity

The constant Double.POSITIVE_INFINITY holds the positive infinity value of type double. This value is obtained by dividing 1.0 by 0.0. Its String representation is Infinity. This value is a convention, and its hexadecimal representation is 7FF0000000000000. Every double variable with this bitwise value contains positive infinity.

4. Negative Infinity

The constant Double.NEGATIVE_INFINITY holds the negative infinity value of type double. This value is obtained by dividing -1.0 by 0.0. Its String representation is -Infinity. This value is also a convention, and its hexadecimal representation is FFF0000000000000. Every double variable with this bitwise value contains negative infinity.

5. Operations with Infinity

Let's declare a double variable called positiveInfinity and assign to it the value Double.POSITIVE_INFINITY and another double variable negativeInfinity and assign to it value Double.NEGATIVE_INFINITY. Then, we get the following results for the operations:

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
        
assertEquals(Double.NaN, (positiveInfinity + negativeInfinity));
assertEquals(Double.NaN, (positiveInfinity / negativeInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveInfinity));
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeInfinity));

Here, the constant Double.NaN represents a result that is not a number.

Let's see the mathematical operations with infinity and a positive number:

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double positiveNumber = 10.0; 
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + positiveNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - positiveNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity * positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity * positiveNumber));
       
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity / positiveNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity / positiveNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (positiveNumber - negativeInfinity));
        
assertEquals(0.0, (positiveNumber / positiveInfinity));
assertEquals(-0.0, (positiveNumber / negativeInfinity));

Now, let's see the mathematical operations with infinity and a negative number:

Double positiveInfinity = Double.POSITIVE_INFINITY;
Double negativeInfinity = Double.NEGATIVE_INFINITY;
double negativeNumber = -10.0; 
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity + negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity + negativeNumber));
        
assertEquals(Double.POSITIVE_INFINITY, (positiveInfinity - negativeNumber));
assertEquals(Double.NEGATIVE_INFINITY, (negativeInfinity - negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity * negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity * negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (positiveInfinity / negativeNumber));
assertEquals(Double.POSITIVE_INFINITY, (negativeInfinity / negativeNumber));
        
assertEquals(Double.NEGATIVE_INFINITY, (negativeNumber - positiveInfinity));
assertEquals(Double.POSITIVE_INFINITY, (negativeNumber - negativeInfinity));
        
assertEquals(-0.0, (negativeNumber / positiveInfinity));
assertEquals(0.0, (negativeNumber / negativeInfinity));

There are a few rules of thumb to remember these operations better:

  • Replace the negative and positive infinities with Infinity and -Infinity, respectively, and perform the sign operations first
  • For any operation between a number different from zero and infinity, you will get a result of infinity
  • When we add or divide positive and negative infinity, we do get not a number result

6. Division by Zero

The division by zero is a special case of division because it produces negative and positive infinity values.

To exemplify, let's take a double value d and check the results of the following divisions by zero:

double d = 1.0;
        
assertEquals(Double.POSITIVE_INFINITY, (d / 0.0));
assertEquals(Double.NEGATIVE_INFINITY, (d / -0.0));
assertEquals(Double.NEGATIVE_INFINITY, (-d / 0.0));
assertEquals(Double.POSITIVE_INFINITY, (-d / -0.0));

7. Conclusion

In this article, we explored the concept and usage of positive and negative infinity in Java. The implementation and code snippets can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!