当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。