Java的Locale equals() 方法和示例
Java中Locale 类的equals()方法用于简单地检查两个语言环境对象之间的相等性。如果语言环境对象在从国家到语言和其他变体的所有方面都相等,则该方法返回 true。
句法:
First_Locale.clone(Sec_Locale)
参数:此方法采用一个参数 Sec_Locale,它是比较的参考对象。
返回值:如果语言环境与 ese false 匹配,则此方法返回布尔值 true。
下面的程序说明了 equals() 方法的工作:
方案一:
// 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));
}
}
输出:
First Locale: en_US
Second Locale: en_US
true
方案二:
// 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));
}
}
输出:
First Locale: ar_SA
Second Locale: en_GB
false