當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Locale equals()用法及代碼示例


Java中的Locale類的equals()方法用於簡單地檢查兩個語言環境對象之間的相等性。如果區域設置對象在從國家到語言以及其他變體的所有方麵都相等,則該方法返回true。

用法:

First_Locale.clone(Sec_Locale)

參數:此方法采用一個參數Sec_Locale,它是比較的參考對象。


返回值:如果語言環境與ese匹配,則此方法返回boolean true。

以下示例程序旨在說明equals()方法的用法:

示例1:

// 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

示例2:

// 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


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 Locale equals() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。