NumberFormat equals() method in Java with Examples

The equals() method is a built-in method of the java.text.NumberFormat accepts an argument which is an object and returns true if this argument object is same as the object, else it returns false.

Syntax:

public boolean equals(Object arg)

Parameters: The function accepts a single mandatory parameter arg which specifies the object which is to be compared with.

Return Value: The function returns a boolean value. It returns true if both the objects are same else it returns false.

Below is the implementation of the above function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
import java.text.NumberFormat;
  
public class Main {
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the Currency Instance
        NumberFormat nF1
            = NumberFormat
                  .getCurrencyInstance();
  
        // Get the Currency Instance
        NumberFormat nF2
            = NumberFormat
                  .getCurrencyInstance();
  
        // Check if equal or not
        if (nF1.equals(nF2))
            System.out.println("Yes both are equal");
        else
            System.out.println("Yes both are not equal");
    }
}

chevron_right


Output:

Yes both are equal

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above function
import java.text.NumberFormat;
  
public class Main {
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the Currency Instance
        NumberFormat nF1
            = NumberFormat
                  .getCurrencyInstance();
  
        // Get the Instance
        NumberFormat nF2
            = NumberFormat
                  .getInstance();
  
        // Check if equal or not
        if (nF1.equals(nF2))
            System.out.println("Yes both are equal");
        else
            System.out.println("both are not equal");
    }
}

chevron_right


Output:

both are not equal

Reference: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#equals(java.lang.Object)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.