Java中的数字包装器类及其方法是什么?

java.lang包的Number类(抽象)表示可转换为原始类型byte,double,float,int,long和short的数值。

以下是java.lang包Number类提供的方法。

序号方法与说明
1

字节 byteValue()

此方法以字节为单位返回指定数字的值。

2

抽象双 doubleValue()

此方法以双精度值形式返回指定数字的值。

3

抽象浮点数 floatValue()

此方法以浮点数形式返回指定数字的值。

4

抽象整数 intValue()

此方法以int形式返回指定数字的值。

5

抽象长 longValue()

此方法以long形式返回指定数字的值。

6

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