当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DoubleAccumulator toString()用法及代码示例


Java.DoubleAccumulator.toString()是java中的内置方法,该方法返回当前值的String表示形式。数字值被显式转换为初始数据类型为double的字符串。

用法:

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


相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 DoubleAccumulator toString() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。