Java.math.MathContext.equals() Method



The java.math.MathContext.equals(Object x) compares this MathContext with the specified Object for equality.


Following is the declaration for java.math.MathContext.equals() method.

public boolean equals(Object x)


equals in class Object.


x − Object to which this MathContext is to be compared.

Return Value

This method returns true if and only if the specified Object is a MathContext object which has exactly the same settings as this object.




The following example shows the usage of math.MathContext.equals() method.

package com.tutorialspoint;

import java.math.*;

public class MathContextDemo {

   public static void main(String[] args) {

      // create 3 MathContext objects
      MathContext mc1, mc2, mc3;

      // assign context settings to mc1, mc2, mc3
      mc1 = new MathContext(2);
      mc2 = new MathContext(2, RoundingMode.HALF_UP);
      mc3 = new MathContext(2, RoundingMode.HALF_DOWN);

      // create 2 boolean objects
      Boolean b1, b2;

      // compare context settings of mc1 with mc2, mc3
      b1 = mc1.equals(mc2);
      b2 = mc1.equals(mc3);

      String str1 = "Context settings of mc1 and mc2 are equal is " + b1;
      String str2 = "Context settings of mc1 and mc3 are equal is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );

Let us compile and run the above program, this will produce the following result −

Context settings of mc1 and mc2 are equal is true
Context settings of mc1 and mc3 are equal is false