Locale equals() Method in Java with Examples

The equals() Method of Locale class in Java is used to simply check the equality between two locale objects. The method returns true if the locale objects are equal at all aspects from country to language and other variants.

Syntax:

First_Locale.clone(Sec_Locale)

Parameters: This method takes one parameter Sec_Locale which is the reference object for the comparison.

Return Value: This method returns boolean true if the locale matches ese false.

Below programs illustrate the working of equals() method:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate equals() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("en", "US");
        Locale sec_locale
            = new Locale("en", "US");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Displaying second locale
        System.out.println("Second Locale: "
                           + sec_locale);
  
        // Displaying the equality
        System.out.println(first_locale
                               .equals(sec_locale));
    }
}

chevron_right


Output:

First Locale: en_US
Second Locale: en_US
true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate equals() method
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("ar", "SA");
        Locale sec_locale
            = new Locale("en", "GB");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Displaying second locale
        System.out.println("Second Locale: "
                           + sec_locale);
  
        // Displaying the equality
        System.out.println(first_locale
                               .equals(sec_locale));
    }
}

chevron_right


Output:

First Locale: ar_SA
Second Locale: en_GB
false


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.