Java DoubleAdder add()方法及示例

来源:这里教程网 时间:2026-02-17 20:47:29 作者:

Java DoubleAdder add()方法及示例

java.DoubleAdder.add() 是java中的一个内置方法,它将给定的值添加到前一个或初始值中。当该类的对象被创建时,其初始值为零。

语法

public void add(double x)

参数: 该方法接受一个单一的参数 x ,指定要添加的值。

返回值: 该方法返回加法操作后的新值。

下面的程序说明了上述函数。

程序1 :

// Program to demonstrate the add() method  import java.lang.*;import java.util.concurrent.atomic.DoubleAdder;  public class GFG {    public static void main(String args[])    {        DoubleAdder num = new DoubleAdder();          // add operation on num        num.add(42);        num.add(10);          // Print after add operation        System.out.println(" the current value is: " + num);    }}

输出:

the current value is: 52.0

程序2 :

// Program to demonstrate the add() method  import java.lang.*;import java.util.concurrent.atomic.DoubleAdder;  public class GFG {    public static void main(String args[])    {        DoubleAdder num = new DoubleAdder();          // add operation on num        num.add(1);          // Print after add operation        System.out.println(" the current value is: " + num);    }}

输出:

the current value is: 1.0

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/DoubleAdder.html#add-double-

相关推荐