Java 中的数字包装类及其方法是什么?
Number 类(抽象)属于 java.lang 包,它表示可以转换为基本类型 byte、double、float、int、long 和 short 的数值。
以下是 java.lang 包的 Number 类提供的方法。
| 序号 | 方法和描述 |
|---|---|
| 1 | byte byteValue() 此方法将指定数字的值作为 byte 返回。 |
| 2 | abstract double doubleValue() 此方法将指定数字的值作为 double 返回。 |
| 3 | abstract float floatValue() 此方法将指定数字的值作为 float 返回。 |
| 4 | abstract int intValue() 此方法将指定数字的值作为 int 返回。 |
| 5 | abstract long longValue() 此方法将指定数字的值作为 long 返回。 |
| 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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP