Java中的 LongAccumulator toString() 方法及示例
Java.LongAccumulator.toString()是Java中的一个内置方法,它返回 LongAccumulator 实例当前值的字符串表示形式。
句法:
public String toString()
参数:此方法不接受任何参数。
返回值:此方法返回当前值的字符串表示形式。
下面的程序说明了上述方法:
程序 1 :
// Program to demonstrate the toString() method
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
public class GFG {
public static void main(String args[])
{
LongAccumulator num = new LongAccumulator(Long::sum, 0L);
// accumulate operation on num
num.accumulate(42);
num.accumulate(10);
num.get();
// before toString the value is
System.out.println(" the old value is: " + num);
;
// toStrings current value
num.toString();
// Print after toString operation
System.out.println(" the current value is: " + num);
}
}
输出:
the old value is: 52
the current value is: 52
方案二:
// program to demonstrate the toString() method
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
public class GFG {
public static void main(String args[])
{
LongAccumulator num = new LongAccumulator(Long::sum, 0L);
// accumulate operation on num
num.accumulate(5);
num.accumulate(10);
num.get();
// before toString the value is
System.out.println(" the old value is: " + num);
// toStrings current value
num.toString();
// Print after toString operation
System.out.println(" the current value is: " + num);
}
}
输出:
the old value is: 15
the current value is: 15
参考:https: Java/util/concurrent/atomic/LongAccumulator.html#toString–