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


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


java.text.MessageFormat類的equals()方法用於檢查兩個MessageFormat對象是否相同。

用法:

public boolean equals(Object obj)

參數:此方法采用MessageFormat對象,該對象將與當前MessageFormat對象進行比較。


返回值:如果兩個MessageFormat對象彼此相等,則它將返回true,否則返回false。

下麵是說明equals()方法的示例:

範例1:

// 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() mehtod 
            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); 
        } 
    } 
}
輸出:
mf1 is equals to mf2

範例2:

// 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() mehtod 
            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); 
        } 
    } 
}
輸出:
mf1 is not equal to mf2

參考: https://docs.oracle.com/javase/9/docs/api/java/text/MessageFormat.html#equals-java.lang.Object-



相關用法


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