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