Java中的 DoubleAdder sum() 方法及示例
Java.DoubleAdder.sum()是Java中的一个内置方法,它返回当前总和。创建类的对象时,其初始值为零。
句法:
public double sum()
参数:此方法不接受任何参数。
返回值:此方法返回当前总和。
下面的程序说明了上述方法:
程序 1 :
// Program to demonstrate the sum() 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);
// sum operation on variable num
num.sum();
// Print after add operation
System.out.println(" the current value is: " + num);
}
}
输出:
the current value is: 52.0
方案二:
// Program to demonstrate the sum() 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(10);
// sum operation on variable num
num.sum();
// Print after add operation
System.out.println(" the current value is: " + num);
}
}
输出:
the current value is: 10.0
参考:https: Java/util/concurrent/atomic/DoubleAdder.html#sum–