Java AtomicInteger floatValue()方法及示例
java.util.concurrent.atomic.AtomicInteger.floatValue() 是java中的一个内置方法,它在进行原始转换后将AtomicInteger的当前值返回为 Float 数据类型。
语法
public float floatValue()
参数: 该函数不接受任何参数。
返回值: 该函数在转换为float类型后返回该对象所代表的数字值。
下面的程序演示了这个函数。
程序 1 :
// Java Program to demonstrates
// the floatValue() function
import java.util.concurrent.atomic.AtomicInteger;
public class GFG {
public static void main(String args[])
{
// Initially value as 0
AtomicInteger val
= new AtomicInteger(0);
val.addAndGet(7);
// Prints the updated value
System.out.println("Previous value: "
+ val);
// Gets the float value
float res = val.floatValue();
System.out.println("float value: "
+ res);
}
}
输出。
Previous value: 7
float value: 7.0
程序2。
// Java Program to demonstrates
// the floatValue() function
import java.util.concurrent.atomic.AtomicInteger;
public class GFG {
public static void main(String args[])
{
// Initially value as 18
AtomicInteger val
= new AtomicInteger(18);
val.addAndGet(7);
// Gets the float value
System.out.println("Previous value: "
+ val);
// Decreases the value by 1
float res = val.floatValue();
System.out.println("float value: "
+ res);
}
}
输出。
Previous value: 25
float value: 25.0
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html#floatValue-
极客教程