Java Utility Library

Java Locale - equals() Method



The java.util.Locale.equals() method returns true if this Locale is equal to another object. A Locale is deemed equal to another Locale with identical language, script, country, variant and extensions, and unequal to all other objects.

Syntax

public boolean equals(Object obj)

Parameters

obj Specify the reference object with which to compare.

Return Value

Returns true if this Locale is equal to the specified object.

Exception

NA

Example:

In the example below, the java.util.Locale.equals() method is used to check if the given Locale is equal to another object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating locales
    Locale loc1 = new Locale("EN", "US");
    Locale loc2 = new Locale("EN", "US");
    Locale loc3 = new Locale("EN", "UK");

    //printing locales
    System.out.println("loc1 is: " + loc1);
    System.out.println("loc2 is: " + loc2);
    System.out.println("loc3 is: " + loc3);

    //checking loc1 and loc2 for equality
    System.out.println("Are loc1 and loc2 equal?: " + loc1.equals(loc2));

    //checking loc1 and loc3 for equality
    System.out.println("Are loc1 and loc3 equal?: " + loc1.equals(loc3));
  }
}

The output of the above code will be:

loc1 is: en_US
loc2 is: en_US
loc3 is: en_UK
Are loc1 and loc2 equal?: true
Are loc1 and loc3 equal?: false

❮ Java.util - Locale