Java
Programming
Infinity
Coding Tips
Software Development

How to implement infinity in Java?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Java already has built-in representations for positive and negative infinity, but only for floating-point types. If you need an “unbounded” sentinel in Java, the right solution depends on whether you are working with double, float, integers, or domain values where a fake numeric maximum would be misleading.

Use the Built-In Floating-Point Constants

For floating-point code, Java follows IEEE 754. That means you can use these constants directly:

  • 'Double.POSITIVE_INFINITY'
  • 'Double.NEGATIVE_INFINITY'
  • 'Float.POSITIVE_INFINITY'
  • 'Float.NEGATIVE_INFINITY'

Example:

java
1public class InfinityDemo {
2    public static void main(String[] args) {
3        double best = Double.POSITIVE_INFINITY;
4        double score = 42.5;
5
6        if (score < best) {
7            best = score;
8        }
9
10        System.out.println(best);
11        System.out.println(Double.isInfinite(best));
12    }
13}

This is common in algorithms such as Dijkstra, shortest-path search, and dynamic programming where a value starts as “no upper bound found yet.”

How Infinity Behaves

Infinity is not just a large number. It has special arithmetic rules.

java
1public class InfinityMath {
2    public static void main(String[] args) {
3        System.out.println(1.0 / 0.0);                    // Infinity
4        System.out.println(-1.0 / 0.0);                   // -Infinity
5        System.out.println(Double.POSITIVE_INFINITY + 5); // Infinity
6        System.out.println(Double.POSITIVE_INFINITY > 1e308); // true
7        System.out.println(Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY); // NaN
8    }
9}

The last line matters. Some operations involving infinity produce NaN, not another infinity. That is why comparisons and validation checks are still important.

What About int and long

Integer types do not have an infinity value. If you are working with int or long, you have three main options:

  • use a very large sentinel such as Integer.MAX_VALUE
  • switch to double if infinity semantics are mathematically appropriate
  • represent “no bound” explicitly with a separate flag or Optional

For many algorithms, Integer.MAX_VALUE is acceptable, but be careful with arithmetic:

java
1int distance = Integer.MAX_VALUE;
2
3// Bad: this overflows
4// distance = distance + 1;

If you use a sentinel maximum, always guard before adding to it.

A Better Pattern for Algorithms

For weighted graph code, floating-point infinity is often clearer than a fake integer maximum. For example:

java
1import java.util.Arrays;
2
3public class Distances {
4    public static void main(String[] args) {
5        double[] dist = new double[4];
6        Arrays.fill(dist, Double.POSITIVE_INFINITY);
7        dist[0] = 0.0;
8
9        System.out.println(Arrays.toString(dist));
10    }
11}

This expresses the intent directly: every node is unreachable until proved otherwise.

If you truly need exact integer arithmetic, a sentinel maximum still works, but the code must treat it as a special case rather than a real distance.

When Not to Use Infinity

Do not use numeric infinity when the meaning is really “missing value,” “not computed,” or “not applicable.” In those cases, an explicit nullable field, Optional, or separate state variable is easier to reason about.

Infinity is also unavailable in BigDecimal. If you need arbitrary precision decimal arithmetic, model unboundedness separately rather than trying to fake it inside the value type.

Common Pitfalls

The most common mistake is assuming integers support infinity the same way floating-point numbers do. They do not.

Another mistake is using Integer.MAX_VALUE and then performing arithmetic on it without overflow checks.

A third mistake is forgetting that Infinity - Infinity and similar operations produce NaN. If you rely on special values, also use Double.isInfinite() and Double.isNaN().

Summary

  • Java has built-in infinity values for double and float, not for integer types.
  • Use Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY when floating-point semantics fit the problem.
  • For int and long, use a guarded sentinel or represent “unbounded” separately.
  • Infinity is useful in algorithms, but it is not the same as a normal very large number.
  • Always watch for overflow with integer sentinels and NaN with floating-point operations.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.