Java中的 LongAccumulator intValue() 方法及示例
Java.LongAccumulator.intValue()是Java中的一个内置方法,它在缩小原始转换后将当前值作为 int 返回。
句法:
public int intValue()
参数:该方法不接受任何参数。
返回值:该方法在缩小原始转换后将当前值作为 int 返回。
下面的程序说明了上述方法:
程序 1 :
// Program to demonstrate the intValue() 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);
// Print before intValue operation
System.out.println(" the old value is: " + num);
// intValues current value
num.intValue();
// Print after intValue operation
System.out.println("the current value is: " + num);
}
}
输出:
the old value is: 52
the current value is: 52
方案二:
// Program to demonstrate the intValue() 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(4);
num.accumulate(4);
// Print before intValue operation
System.out.println(" the old value is: " + num);
// intValues current value
num.intValue();
// Print after intValue operation
System.out.println(" the current value is: " + num);
}
}
输出:
the old value is: 8
the current value is: 8
参考:https: Java/util/concurrent/atomic/LongAccumulator.html#intValue–