Java Float intValue()方法及示例

来源:这里教程网 时间:2026-02-17 20:55:18 作者:

Java Float intValue()方法及示例

Float类中的 intValue() 方法是Java中的一个内置方法,在类型转换后返回调用对象指定的int值。

语法

public int intValue()

参数 :该函数不接受任何参数。

返回值: 它返回FloatObject的值为int。

以下程序说明了Java中的intValue()方法。

程序1 :

// Java code to demonstrate// Float intValue() methodclass GFG {    public static void main(String[] args)    {          // Float value        Float a = 17.47f;          // wrapping the Float value        // in the wrapper class Float        Float b = new Float(a);          // intValue of the Float Object        int output = b.intValue();          // printing the output        System.out.println("Int value of "                           + b + " is : " + output);    }}

输出。

Int value of 17.47 is : 17

程序2

// Java code to demonstrate// Float intValue() method// Not a numberclass GFG {    public static void main(String[] args)    {          // Float value        Float a = Float.NaN;          // wrapping the Float value        // in the wrapper class Float        Float b = new Float(a);          // intValue of the Float Object        int output = b.intValue();          // printing the output        System.out.println("Int value of "                           + b + " is : " + output);    }}

输出。

Int value of NaN is : 0

参考资料: https: //docs.oracle.com/javase/7/docs/api/java/lang/Float.html#intValue()

相关推荐