📜  Java中的 NumberFormat equals() 方法及示例

📅  最后修改于: 2022-05-13 01:55:31.798000             🧑  作者: Mango

Java中的 NumberFormat equals() 方法及示例

equals()方法是Java.text.NumberFormat的内置方法,它接受一个对象参数,如果该参数对象与对象相同,则返回 true,否则返回 false。

语法

public boolean equals(Object arg)

参数:该函数接受一个强制参数arg ,该参数指定要与之比较的对象。

返回值:该函数返回一个布尔值。如果两个对象相同,则返回 true,否则返回 false。

下面是上述函数的实现:

方案一:

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

方案二:

// 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: Java Java)