Java xxxValue() 方法
xxxValue() 方法用於將 Number 對象轉換為 xxx 數據類型的值並返回。
相關的方法有:
類型 | 方法及描述 |
---|---|
byte | byteValue() : 以 byte 形式返回指定的數值。 |
abstract double | doubleValue() : 以 double 形式返回指定的數值。 |
abstract float | floatValue() : 以 float 形式返回指定的數值。 |
abstract int | intValue() : 以 int 形式返回指定的數值。 |
abstract long | longValue() : 以 long 形式返回指定的數值。 |
short | shortValue() : 以 short 形式返回指定的數值。 |
參數
以上各函數不接受任何的參數。
返回值
轉換為 xxx 類型後該對象表示的數值。
實例
Test.java 檔
public class Test{
public static void main(String args[]){
Integer x = 5;
// 返回 byte 原生數據類型
System.out.println( x.byteValue() );
// 返回 double 原生數據類型
System.out.println(x.doubleValue());
// 返回 long 原生數據類型
System.out.println( x.longValue() );
}
}
編譯以上程式,輸出結果為:
5 5.0 5