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