在 Java 中將 Long 轉換為數字基本資料型別
假設我們有一個 Long 物件。
Long myObj = new Long("9879");現在,如果我們要將此 Long 轉換為 short 基本資料型別。為此,請使用內建 shortValue() 方法 −
// converting to short primitive types short shortObj = myObj.shortValue(); System.out.println(shortObj);
以相同的方式將 Long 轉換為另一個數字基本資料型別 int。為此,請使用內建 intValue() 方法 −
// converting to int primitive types int intObj = myObj.intValue(); System.out.println(intObj);
以下是一個示例,其中我們將 Long 轉換為數字基本型別 short、int、float 等 −
示例
public class Demo {
public static void main(String[] args) {
Long myObj = new Long("9879");
// converting to numeric primitive types
short shortObj = myObj.shortValue();
System.out.println(shortObj);
int intObj = myObj.intValue();
System.out.println(intObj);
float floatObj = myObj.floatValue();
System.out.println(floatObj);
double doubleObj = myObj.doubleValue();
System.out.println(doubleObj);
}
}輸出
9879 9879 9879.0 9879.0
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP