Thursday, June 11, 2026
HomeLanguagesJavaMessageFormat equals() method in Java with Example

MessageFormat equals() method in Java with Example

The equals() method of java.text.MessageFormat class is used to check if both the MessageFormat objects are same or not.
 

Syntax:  

public boolean equals(Object obj)

Parameter: This method takes MessageFormat objects which will be compared with the current MessageFormat object.
Return Value: if both the MessageFormat objects are equal to each other then it will return true otherwise false.
Below are the examples to illustrate the equals() method:
Example 1: 
 

Java




// Java program to demonstrate
// equals() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // creating and initializing  MessageFormat
            MessageFormat mf1
                = new MessageFormat("{0, date, #}, {1, time, #}, {0, number}");
 
            // creating and initializing  MessageFormat
            MessageFormat mf2
                = new MessageFormat("{0, date, #}, {1, time, #}, {0, number}");
 
            // compare both object
            // using equals() method
            boolean i = mf1.equals(mf2);
 
            // display result
            if (i)
                System.out.println(
                    "mf1 is equals to mf2");
            else
                System.out.println(
                    "mf1 is not equal to mf2");
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

mf1 is equals to mf2

 

Example 2: 
 

Java




// Java program to demonstrate
// equals() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        try {
 
            // creating and initializing  MessageFormat
            MessageFormat mf1
                = new MessageFormat("{0, date, #}, {1, time, #}, {0, number}");
 
            // creating and initializing  MessageFormat
            MessageFormat mf2
                = new MessageFormat("{0, date, #}, {0, time, #}, {0, number}");
 
            // compare both object
            // using equals() method
            boolean i = mf1.equals(mf2);
 
            // display result
            if (i)
                System.out.println(
                    "mf1 is equals to mf2");
            else
                System.out.println(
                    "mf1 is not equal to mf2");
        }
 
        catch (ClassCastException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output: 

mf1 is not equal to mf2

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#equals-java.lang.Object-

RELATED ARTICLES

1 COMMENT

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