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() |
该方法以int的形式返回指定数字的值。 |
| 5 | abstract long longValue() |
该方法将指定数字的值作为一个长数返回。 |
| 6 | short shortValue() |
该方法将指定数字的值作为一个短值返回。 |
例子
public class NumberClassExample {
public static void main(String args[]){
Number num = new Integer("250");
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: 250.0
Double value of the number: 28.0
Long value of the number: 250
Byte value of the number: 250
Double value of the number: 250.0
Short value of the number: 250
极客教程