Java DoubleAccumulator toString()方法及示例
Java.DoubleAccumulator.toString() 是java中的一个内置方法,用于返回当前值的字符串表示。数值被明确地转换为字符串,其中初始数据类型为双数。
语法
public String toString()
参数: 该方法不接受任何参数。
返回值: 该方法返回当前值的字符串表示。
下面的程序说明了上述方法。
程序1 :
// Java program to demonstrate
// the toString() method
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
public class GFG {
public static void main(String args[])
{
DoubleAccumulator num
= new DoubleAccumulator(
Double::sum, 0L);
// accumulate operation on num
num.accumulate(42);
num.accumulate(10);
// before toString the value is
System.out.println("DoubleAccumulator: "
+ num.get());
// Print String representation
System.out.println("String representation: "
+ num.toString());
}
}
输出:
DoubleAccumulator: 52.0
String representation: 52.0
程序2
// Java program to demonstrate
// the toString() method
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
public class GFG {
public static void main(String args[])
{
DoubleAccumulator num
= new DoubleAccumulator(
Double::sum, 0L);
// accumulate operation on num
num.accumulate(75);
num.accumulate(1);
// before toString the value is
System.out.println("DoubleAccumulator: "
+ num.get());
// Print String representation
System.out.println("String representation: "
+ num.toString());
}
}
输出:
DoubleAccumulator: 76.0
String representation: 76.0