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


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


equals()方法是java.text.NumberFormat的內置方法,接受作為對象的參數,如果該參數對象與該對象相同,則返回true,否則返回false。

用法

public boolean equals(Object arg)

參數:該函數接受單個強製參數arg,該參數指定要與之進行比較的對象。


返回值:該函數返回一個布爾值。如果兩個對象相同,則返回true,否則返回false。

下麵是上述函數的實現:

示例1:

// Java program to implement 
// the above function 
import java.text.NumberFormat; 
  
public class Main { 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        // Get the Currency Instance 
        NumberFormat nF1 
            = NumberFormat 
                  .getCurrencyInstance(); 
  
        // Get the Currency Instance 
        NumberFormat nF2 
            = NumberFormat 
                  .getCurrencyInstance(); 
  
        // Check if equal or not 
        if (nF1.equals(nF2)) 
            System.out.println("Yes both are equal"); 
        else
            System.out.println("Yes both are not equal"); 
    } 
}
輸出:
Yes both are equal

示例2:

// Java program to implement 
// the above function 
import java.text.NumberFormat; 
  
public class Main { 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        // Get the Currency Instance 
        NumberFormat nF1 
            = NumberFormat 
                  .getCurrencyInstance(); 
  
        // Get the Instance 
        NumberFormat nF2 
            = NumberFormat 
                  .getInstance(); 
  
        // Check if equal or not 
        if (nF1.equals(nF2)) 
            System.out.println("Yes both are equal"); 
        else
            System.out.println("both are not equal"); 
    } 
}
輸出:
both are not equal

參考: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#equals(java.lang.Object)



相關用法


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