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


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


java.LongAccumulator.toString()是java中的一个内置方法,该方法返回LongAccumulator实例的当前值的String表示形式。

用法:

public String toString()

参数:此方法不接受任何参数。


返回值:此方法返回当前值的字符串表示形式。

以下示例程序旨在说明上述方法:

程序1

// Program to demonstrate the toString() method 
  
import java.lang.*; 
import java.util.concurrent.atomic.LongAccumulator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        LongAccumulator num = new LongAccumulator(Long::sum, 0L); 
  
        // accumulate operation on num 
        num.accumulate(42); 
        num.accumulate(10); 
  
        num.get(); 
        // before toString the value is 
        System.out.println(" the old value is: " + num); 
        ; 
  
        // toStrings current value 
        num.toString(); 
  
        // Print after toString operation 
        System.out.println(" the current value is: " + num); 
    } 
}
输出:
the old value is: 52
 the current value is: 52

程序2

// program to demonstrate the toString() method 
  
import java.lang.*; 
import java.util.concurrent.atomic.LongAccumulator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        LongAccumulator num = new LongAccumulator(Long::sum, 0L); 
  
        // accumulate operation on num 
        num.accumulate(5); 
        num.accumulate(10); 
  
        num.get(); 
        // before toString the value is 
        System.out.println(" the old value is: " + num); 
  
        // toStrings current value 
        num.toString(); 
  
        // Print after toString operation 
        System.out.println(" the current value is: " + num); 
    } 
}
输出:
the old value is: 15
 the current value is: 15

参考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/LongAccumulator.html#toString–



相关用法


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