在 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