Java 中的數字包裝類及其方法是什麼?


Number 類(抽象)屬於 java.lang 包,它表示可以轉換為基本型別 byte、double、float、int、long 和 short 的數值。

以下是 java.lang 包Number 類提供的方法。

序號 方法和描述
1

byte byteValue()

此方法將指定數字的值作為 byte 返回。

2

abstract double doubleValue()

此方法將指定數字的值作為 double 返回。

3

abstract float floatValue()

此方法將指定數字的值作為 float 返回。

4

abstract int intValue()

此方法將指定數字的值作為 int 返回。

5

abstract long longValue()

此方法將指定數字的值作為 long 返回。

6

short shortValue()

此方法將指定數字的值作為 short 返回。

示例

線上演示

public class NumberClassExample {
   public static void main(String args[]){
      Number num = new Integer("25");
      System.out.println("Float value of the number: "+num.floatValue());
      System.out.println("Double value of the number: "+num.doubleValue());
      System.out.println("Long value of the number: "+num.longValue());
      System.out.println("Byte value of the number: "+num.byteValue());
      System.out.println("Double value of the number: "+num.doubleValue());
      System.out.println("Short value of the number: "+num.shortValue());
   }
}

輸出

Float value of the number: 25.0
Double value of the number: 25.0
Long value of the number: 25
Byte value of the number: 25
Double value of the number: 25.0
Short value of the number: 25

更新於: 2019-07-30

354 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告