Java MathContext Class | equals() Method with Example

MathContext Class equals() method: Here, we are going to learn about the equals() method of MathContext Class with its syntax and example.
Submitted by Preeti Jain, on May 12, 2020

MathContext Class equals() method

  • equals() method is available in java.math package.
  • equals() method is used to check whether this MathContext and the given object are equal or not.
  • equals() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • equals() method does not throw an exception at the time of comparing two objects.

Syntax:

    public boolean equals(Object o);

Parameter(s):

  • Object o – represents the object is to be compared to this object.

Return value:

The return type of this method is boolean, it returns true when both the compared objects are equal in terms of context settings otherwise it returns false.

Example:

// Java program to demonstrate the example 
// of boolean equals(Object o) method of MathContext

import java.math.*;

public class EqualsOfMC {
    public static void main(String args[]) {
        // Initialize three MathContext objects  
        MathContext ma_co1 = new MathContext(3, RoundingMode.HALF_UP);
        MathContext ma_co2 = new MathContext(3);
        MathContext ma_co3 = new MathContext(3, RoundingMode.FLOOR);

        // compares this MathContext (ma_co1) to the given
        // MathContext (ma_co2) for equality and here it returns
        // true because they both are of same type
        // same settings
        boolean compare = ma_co1.equals(ma_co2);
        System.out.println("ma_co1.equals(ma_co2): " + compare);

        // compares this MathContext (ma_co2) to the given
        // MathContext (ma_co3) for equality and here it returns
        // false because they both are of same type
        // but different settings
        compare = ma_co2.equals(ma_co3);
        System.out.println("ma_co2.equals(ma_co3): " + compare);
    }
}

Output

ma_co1.equals(ma_co2): true
ma_co2.equals(ma_co3): false


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.