當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。