什麼是 Java 中的數字包裝器類及其方法?
java.lang 包的Number 類(抽象)表示可轉換為基本型別 byte、double、float、int、long 和 short 的數字值。
以下是 java.lang 包的 Number 類提供的方法。
| 序號 | 方法和說明 |
|---|---|
| 1 | byte byteValue() 此方法將指定數字的值作為位元組返回。 |
| 2 | abstract double doubleValue() 此方法將指定數字的值作為雙精度值返回。 |
| 3 | abstract float floatValue() 此方法將指定數字的值作為浮點值返回。 |
| 4 | abstract int intValue() 此方法將指定數字的值作為整數返回。 |
| 5 | abstract long longValue() 此方法將指定數字的值作為長整型返回。 |
| 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP