Java DoubleAdder toString()方法及示例

来源:这里教程网 时间:2026-02-17 20:47:31 作者:

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.0the value after toString() is: 52.0

程序2 :

// 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.0the value after toString() is: 402.0

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/DoubleAdder.html#toString-

相关推荐