Open In App

Java MathContext equals() Method

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The equals() method in the Java MathContext class is a part of the java.math package. This method is used to compare one MathContext object with another and checks whether both objects have the same precision and rounding mode settings.

This method is very useful when we want to verify if two MathContext configurations are equal for BigDecimal calculations.

What the MathContext equals() Method Checks

This method performs the following comparison:

  • Precision Match: It checks if both objects have the same number of digits of precision.
  • Rounding Mode Match: It verifies if both objects use the same rounding mode for example, HALF_UP, FLOOR.

If both of these properties match, the method returns true. Otherwise, it returns false.

Syntax of MathContext equals() Method

public boolean equals(Object obj)

  • Parameter: obj: The object to be compared with the current MathContext instance.
  • Returns: It returns "true", if the specified object is also a MathContext with the same settings, otherwise "false".

Examples of Java MathContext equals() Method

Example 1: In this example, we are going to create two MathContext objects with the same precision but different rounding modes and then compare them.

Java
// Java program to demonstrate MathContext equals() method
import java.math.MathContext;
import java.math.RoundingMode;

public class Geeks {

    public static void main(String[] args) {

        // Create two MathContext objects with 
        // same precision, different rounding modes
        MathContext m1 = new MathContext(2);
        MathContext m2 = new MathContext(2, RoundingMode.FLOOR);

        // Compare and print the result
        System.out.println(m1.equals(m2));
    }
}

Output
false


Example 2: In this example, we are going to create two MathContext objects with same precision and the default rounding mode to see if they are considered equal.

Java
// Java program to demonstrate MathContext equals() method
import java.math.MathContext;
import java.math.RoundingMode;

public class Geeks {

    public static void main(String[] args) {

        // Create two MathContext objects 
        // with same settings
        MathContext m1 = new MathContext(2);
        MathContext m2 = new MathContext(2, RoundingMode.HALF_UP);

        // Compare and print the result
        System.out.println(m1.equals(m2));
    }
}

Output
true


When to Use MathContext equals() Method

We can use the equals() method when:

  • We want to validate whether two MathContext objects have the same precision and rounding rules.
  • We are testing or debugging BigDecimal operations and need to confirm the consistency of context.
  • We want to ensure that two modules or configurations use identical MathContext settings before executing calculations.

Reference: https://docs.oracle.com/javase/7/docs/api/java/math/MathContext.html#equals(java.lang.Object)


Practice Tags :

Similar Reads