Thursday, June 11, 2026
HomeLanguagesJavaMathContext equals() Method in Java

MathContext equals() Method in Java

The java.math.MathContext.equals() is an in-built function in Java which checks for equality between this MathContext object with the object passed as parameter to the function. The function returns true if the context settings of both the aforementioned objects are same.

Syntax :

public boolean equals(Object obj)

Parameters : The function accepts an object obj as a mandatory parameter with which the MathContext is checked for equality.

Return Value: This method returns true if and only if the specified Object is a MathContext object with same context settings as this object.

Examples:

Input : m1 = new MathContext(2, RoundingMode.UP), 
        m2 = new MathContext(2, RoundingMode.HALF_UP)
Output : false

Input :  m1 = new MathContext(2), 
         m2 = new MathContext(2, RoundingMode.HALF_UP)
Output : true

Below programs will illustrate the use of java.math.MathContext.equals() :
Program 1 :




// Java program to demonstrate equals() method
import java.math.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating 2 MathContext objects m1 and m2
        MathContext m1, m2;
  
        // Assigning context settings to m1, m2
        m1 = new MathContext(2);
        m2 = new MathContext(2, RoundingMode.FLOOR);
  
        // Displaying the result
        System.out.println(m1.equals(m2));
    }
}


Output:

false

Program 2 :




// Java program to demonstrate equals() method
import java.math.*;
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Creating 2 MathContext objects m1 and m2
        MathContext m1, m2;
  
        // Assigning context settings to m1, m2
        m1 = new MathContext(2);
        m2 = new MathContext(2, RoundingMode.HALF_UP);
  
        // Displaying the result
        System.out.println(m1.equals(m2));
    }
}


Output:

true

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

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS