Java Integer floatValue()方法

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

Java Integer floatValue()方法

java.lang包中Integer类 的floatValue()方法用于将给定Integer的值在扩大的原始转换后转换为float类型,一般情况下,当我们想截断或舍弃整数值时,该方法就很适用。该包的视图如下。

--> java.lang Package    --> Integer Class        --> floatValue() Method

语法

public float floatValue()

返回类型: 该对象转换为float类型后所代表的数值。

注意: 这个方法在java 1.2或更高版本的支持下是可行的,所以如果运行在旧的(介绍性的)java版本,请确保更新java版本。

例1 :

// Java Program to Demonstrate floatValue() Method// of Integer Class // Importing required classesimport java.lang.Integer; // Classclass GFG {     // Main driver method    public static void main(String args[])    {         // Creating a custom integer by        // creating object of Integer class        Integer a = new Integer(28);         // Converting Integer number to float value        // using floatValue() method        float b = a.floatValue();         // Printing the float value        System.out.println(b);    }}

输出

例2 :

// Java Program to Demonstrate Working of floatValue()// Method of Integer Class // Importing required classimport java.lang.Integer; // Classclass GFG {     // Main driver method    public static void main(String args[])    {        // Creating a custom integer value by        // declaring object of Integer Class        Integer a = new Integer(20);         // Convert Integer number to float value        // using floatValue() method        float b = a.floatValue();         // Printing the float value on console        System.out.println(b);    }}

输出

相关推荐