# MessageFormat equals() method in Java with Example

• Last Updated : 13 Sep, 2021

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

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

`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 than 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`

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.5272943,"math_prob":0.8733296,"size":2475,"snap":"2021-43-2021-49","text_gpt3_token_len":628,"char_repetition_ratio":0.15864022,"word_repetition_ratio":0.6021798,"special_character_ratio":0.27555555,"punctuation_ratio":0.21365638,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9625593,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-28T00:55:29Z\",\"WARC-Record-ID\":\"<urn:uuid:2c22f494-9663-4ac8-a686-c43b22f83672>\",\"Content-Length\":\"123781\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b9a69868-81b9-4ebc-9d28-3db4da7f1551>\",\"WARC-Concurrent-To\":\"<urn:uuid:34ab6306-3600-4a4e-bd84-75e306f118f9>\",\"WARC-IP-Address\":\"23.205.106.135\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/messageformat-equals-method-in-java-with-example/?ref=leftbar-rightbar\",\"WARC-Payload-Digest\":\"sha1:REN3JO63MHSXD5XOQJ7X5T2Y4VCOVJ4C\",\"WARC-Block-Digest\":\"sha1:IFEZJ5FE36MTLK23MUNABDLFCDFWDVCJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323588246.79_warc_CC-MAIN-20211028003812-20211028033812-00688.warc.gz\"}"}