Java中的数字包装器类及其方法是什么?
java.lang包的Number类(抽象)表示可转换为原始类型byte,double,float,int,long和short的数值。
以下是java.lang包的Number类提供的方法。
字节byteValue()
此方法以字节为单位返回指定数字的值。
抽象双doubleValue()
此方法以双精度值形式返回指定数字的值。
抽象浮点数floatValue()
此方法以浮点数形式返回指定数字的值。
抽象整数intValue()
此方法以int形式返回指定数字的值。
抽象长longValue()
此方法以long形式返回指定数字的值。
短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