方法 | 描述 |
---|---|
byte byteValue() short shortValue() int intValue() long longValue() float floatValue() double doubleValue() | 將 Number 轉換回基本資料型態 |
舉例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class ValueDemo { public static void main(String[] args) { Byte a1 = 25 ; Short a2 = 24 ; Integer a3 = 23 ; Long a4 = 22l; Float a5 = 21 .0f; Double a6 = 20.0 ; byte b1 = a1.byteValue(); short b2 = a2.shortValue(); int b3 = a3.intValue(); long b4 = a4.longValue(); float b5 = a5.floatValue(); double b6 = a6.doubleValue(); System.out.println(b1 + b2 + b3 + b4 + b5 + b6); } } /* 《程式語言教學誌》的範例程式 檔名:ValueDemo.java 功能:示範物件導向的基本觀念 作者:張凱慶 時間:西元 2010 年 10 月 */ |
編譯後執行,結果如下

其中,第 3 行到第 8 行
3 4 5 6 7 8 | Byte a1 = 25 ; Short a2 = 24 ; Integer a3 = 23 ; Long a4 = 22l; Float a5 = 21 .0f; Double a6 = 20.0 ; |
a1 、 a2 、 a3 、 a4 、 a5 、 a6 分別指定為 Byte 、 Short 、 Integer 、 Long 、 Float 、 Double 等型態的物件 (object) ,底下第 10 行到第 15 行
10 11 12 13 14 15 | byte b1 = a1.byteValue(); short b2 = a2.shortValue(); int b3 = a3.intValue(); long b4 = a4.longValue(); float b5 = a5.floatValue(); double b6 = a6.doubleValue(); |
這會將 a1 、 a2 、 a3 、 a4 、 a5 、 a6 各物件的數值取出,轉換成基本資料型態,然後存入相關變數之中。
中英文術語對照 | |
---|---|
子類別 | subclass |
基本資料型態 | primitive data type |
物件 | object |
沒有留言:
張貼留言