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.
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:
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.
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
doubleif 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:
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:
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
doubleandfloat, not for integer types. - Use
Double.POSITIVE_INFINITYandDouble.NEGATIVE_INFINITYwhen floating-point semantics fit the problem. - For
intandlong, 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
NaNwith floating-point operations.
Related reading
- How to import a .cer certificate into a java keystore?
- How to import a GIT non-Eclipse Java project into Eclipse?
- How to import a jar in Eclipse?
- How to import an existing X.509 certificate and private key in Java keystore to use in SSL?
- How to increase the number of messages consumed by Spring Kafka Consumer in each batch?
- How to initialize a Guava ImmutableMap?
- How to initialize an array in Java?
- How to initialize HashSet values by construction?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.