BigDecimal
comparison operators
Java programming
arithmetic operations
Java BigDecimal

How to use comparison operators like , , on BigDecimal

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

You cannot use primitive comparison operators such as >, <, or == directly with BigDecimal in Java because BigDecimal is an object, not a primitive number type. The normal replacement is compareTo, with equals reserved for the narrower case where both value and scale must match.

Use compareTo for Numeric Ordering

compareTo returns a negative value, zero, or a positive value depending on whether the left-hand side is smaller, equal, or larger than the right-hand side.

java
1import java.math.BigDecimal;
2
3public class BigDecimalCompareDemo {
4    public static void main(String[] args) {
5        BigDecimal a = new BigDecimal("10.0");
6        BigDecimal b = new BigDecimal("12.5");
7
8        System.out.println(a.compareTo(b) < 0);  // true
9        System.out.println(a.compareTo(b) > 0);  // false
10        System.out.println(a.compareTo(b) == 0); // false
11    }
12}

This is the closest equivalent to primitive comparison operators and the method you should use most of the time.

Do Not Confuse compareTo with equals

equals on BigDecimal checks both numeric value and scale. That means values that are numerically equal can still fail equals.

java
1import java.math.BigDecimal;
2
3BigDecimal x = new BigDecimal("100.0");
4BigDecimal y = new BigDecimal("100.00");
5
6System.out.println(x.compareTo(y) == 0); // true
7System.out.println(x.equals(y));         // false

If your domain logic cares only about numeric meaning, use compareTo. If your domain logic also cares about representation and scale, then equals may be the right choice.

Write Readable Helper Methods

Because repeated compareTo(...) > 0 expressions can become noisy, many codebases wrap common comparisons in small helper methods.

java
1import java.math.BigDecimal;
2
3public final class BigDecimals {
4    public static boolean isGreaterThan(BigDecimal left, BigDecimal right) {
5        return left.compareTo(right) > 0;
6    }
7
8    public static boolean isLessThanOrEqual(BigDecimal left, BigDecimal right) {
9        return left.compareTo(right) <= 0;
10    }
11}

That can make business rules read more naturally, especially in financial code where comparisons are everywhere.

Use compareTo in Conditions and Sorting

The same method works for both if statements and collection sorting.

java
1import java.math.BigDecimal;
2import java.util.List;
3
4List<BigDecimal> amounts = List.of(
5    new BigDecimal("4.50"),
6    new BigDecimal("2.00"),
7    new BigDecimal("10.25")
8);
9
10List<BigDecimal> sorted = amounts.stream()
11    .sorted(BigDecimal::compareTo)
12    .toList();
13
14System.out.println(sorted);

Because BigDecimal implements Comparable, many Java APIs already understand how to order it correctly.

Be Explicit About Null Handling

compareTo will throw a NullPointerException if either side is null. If null is possible in your domain model, define the policy clearly:

  • reject null immediately
  • normalize null to zero only if that is a real business rule
  • use comparators such as Comparator.nullsFirst for collection sorting

The important part is to make the rule explicit rather than letting it emerge accidentally from exceptions in production.

Prefer String Constructors for Exact Values

Comparison bugs often begin before the comparison itself. If you create BigDecimal from binary floating-point values, you can introduce surprising precision artifacts. For exact decimal comparisons, prefer string-based construction such as new BigDecimal("10.25").

Common Pitfalls

  • Trying to use >, <, or == directly on BigDecimal references.
  • Using equals when you only care about numeric equality.
  • Ignoring scale differences and then being surprised by equals returning false.
  • Repeating complex compareTo expressions everywhere instead of introducing readable helpers.
  • Silently treating null as zero without confirming that it matches business rules.

Summary

  • Use compareTo instead of primitive comparison operators with BigDecimal.
  • 'compareTo checks numeric ordering, while equals checks numeric value plus scale.'
  • Prefer compareTo(...) == 0 when testing numeric equality.
  • Small helper methods can make business comparisons easier to read.
  • Be deliberate about null handling and scale semantics in financial code.

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.