📌  相关文章
📜  Java中的 OptionalDouble of(double) 方法及示例

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

Java中的 OptionalDouble of(double) 方法及示例

of(double)方法帮助我们获得一个OptionalDouble对象,该对象包含一个作为参数传递给该方法的 double 值。

句法:

public static OptionalDouble of(double value)

参数:此方法接受一个双精度值作为参数,该参数将设置为返回的 OptionalDouble 对象。

返回值:此方法返回具有当前值的OptionalDouble

以下程序说明了 of(double) 方法:
方案一:

// Java program to demonstrate
// OptionalDouble.of(double) method
  
import java.util.OptionalDouble;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a OptionalDouble instance
        // using of() method
        OptionalDouble opDouble
            = OptionalDouble.of(45213.246);
  
        // Get value of this OptionalDouble instance
        System.out.println("OptionalDouble: "
                           + opDouble);
    }
}
输出:
OptionalDouble: OptionalDouble[45213.246]

方案二:

// Java program to demonstrate
// OptionalDouble.of(double) method
  
import java.util.OptionalDouble;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create a OptionalDouble instance
        // using of() method
        OptionalDouble opDouble
            = OptionalDouble.of(21438999);
  
        // Get value of this OptionalDouble instance
        System.out.println("OptionalDouble: "
                           + opDouble);
    }
}
输出:
OptionalDouble: OptionalDouble[2.1438999E7]

参考资料: https: Java/util/OptionalDouble.html#of(double)