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