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


Java OptionalDouble equals()用法及代码示例


OptionalDouble帮助我们创建一个可能包含或可能不包含Double值的对象。 equals(Object obj)方法可帮助我们将此OptionalDouble对象与传递的对象作为参数进行比较,如果对象相等,则返回true。

如果满足以下条件,则认为另一个对象与此OptionalDouble相等:

  • 它也是一个OptionalDouble和;
  • 两个实例都不存在值;或者
  • 当前值通过==彼此为“equal to”。

用法:


public boolean equals(Object obj)

参数:此方法接受obj,它是要测试是否相等的对象。

返回值:如果另一个对象是“equal to”,则此方法返回true;否则返回false。

以下示例程序旨在说明equals(Object obj)方法:

示例1:

// Java program to demonstrate 
// OptionalDouble.equals(Object obj) method 
  
import java.util.OptionalDouble; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create first OptionalDouble object 
        OptionalDouble opDouble1 
            = OptionalDouble.of(452.13246); 
  
        System.out.println("OptionalDouble 1: "
                           + opDouble1.toString()); 
  
        // Create second OptionalDouble object 
        OptionalDouble opDouble2 
            = OptionalDouble.of(452.13246); 
  
        System.out.println("OptionalDouble 2: "
                           + opDouble2.toString()); 
  
        // Check if these two objects are equal 
        // using equals(Object obj) 
        System.out.println("Are both objects equal: "
                           + opDouble1.equals(opDouble2)); 
    } 
}
输出:
OptionalDouble 1: OptionalDouble[452.13246]
OptionalDouble 2: OptionalDouble[452.13246]
Are both objects equal: true

示例2:

// Java program to demonstrate 
// OptionalDouble.equals(Object obj) method 
  
import java.util.OptionalDouble; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // Create first OptionalDouble object 
        OptionalDouble opDouble1 
            = OptionalDouble.of(3179464.92); 
  
        System.out.println("OptionalDouble 1: "
                           + opDouble1.toString()); 
  
        // Create second OptionalDouble object 
        OptionalDouble opDouble2 
            = OptionalDouble.of(4521.3246); 
  
        System.out.println("OptionalDouble 2: "
                           + opDouble2.toString()); 
  
        // Check if these two objects are equal 
        // using equals(Object obj) 
        System.out.println("Are both objects equal: "
                           + opDouble1.equals(opDouble2)); 
    } 
}
输出:
OptionalDouble 1: OptionalDouble[3179464.92]
OptionalDouble 2: OptionalDouble[4521.3246]
Are both objects equal: false

参考文献: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalDouble.html#equals?(Object obj)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 OptionalDouble equals() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。