Java - Byte floatValue() 方法



描述

Java Byte floatValue() 方法返回此 Byte 的浮點型值。

宣告

以下是 java.lang.Byte.floatValue() 方法的宣告

public float floatValue()

指定於

Number 類中的 floatValue

引數

返回值

此方法在轉換為 float 型別後返回此物件表示的數值。

異常

使用 new 運算子建立的 Byte 物件獲取浮點型值示例

以下示例演示了使用 new 運算子建立的 Byte 物件的 Byte floatValue() 方法的使用。我們建立一個 Byte 變數並將其分配給使用 new 運算子建立的 Byte 物件。然後建立一個 float 變數並使用 floatValue() 方法分配一個 float 值,然後列印結果。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte object b
      Byte b;

      // assign value to b
      b = new Byte("100");

      // create a float primitive d
      float d;

      // assign primitive value of b to d
      d = b.floatValue();

      String str = "Primitive float value of Byte object " + b + " is " + d;

      // print d value
      System.out.println( str );
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Primitive float value of Byte object 100 is 100.0

使用 valueOf(String) 方法建立的 Byte 物件獲取浮點型值示例

以下示例演示了使用 valueOf(String) 方法建立的 Byte 物件的 Byte floatValue() 方法的使用。我們建立一個 Byte 變數並將其分配給使用 valueOf(String) 方法建立的 Byte 物件。然後建立一個 float 變數並使用 floatValue() 方法分配一個 float 值,然後列印結果。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte object b
      Byte b;

      // assign value to b
      b = Byte.valueOf("100");

      // create a float primitive d
      float d;

      // assign primitive value of b to d
      d = b.floatValue();

      String str = "Primitive float value of Byte object " + b + " is " + d;

      // print d value
      System.out.println( str );
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Primitive float value of Byte object 100 is 100.0

使用 valueOf(byte) 方法建立的 Byte 物件獲取浮點型值示例

以下示例演示了使用 valueOf(byte) 方法建立的 Byte 物件的 Byte floatValue() 方法的使用。我們建立一個 Byte 變數並將其分配給使用 valueOf(byte) 方法建立的 Byte 物件。然後建立一個 float 變數並使用 floatValue() 方法分配一個 float 值,然後列印結果。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte object b
      Byte b;

      // assign value to b
      b = Byte.valueOf((byte) 100);

      // create a float primitive d
      float d;

      // assign primitive value of b to d
      d = b.floatValue();

      String str = "Primitive byte value of Byte object " + b + " is " + d;

      // print d value
      System.out.println( str );
   }
}

輸出

讓我們編譯並執行以上程式,這將產生以下結果:

Primitive byte value of Byte object 100 is 100.0
java_lang_byte.htm
廣告

© . All rights reserved.