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