Java DoubleAdder toString()方法及示例

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

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

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程