写在前面的话
该系列的参考资料来自开源项目:LearningJDK。感谢作者的贡献。
项目地址:LearningJDK
https://github.com/kangjianwei/LearningJDK
前 言
第一篇,先来看看Number这个类的源码。
Number 相关概念
抽象类Number是数值类的父类,定义了让数值类的数值转换为基础类型(int,double等等)的方法。
这边的数值类指的是:
-
用来表示一个数值。
-
表示的这个数值能够转换成基础类型byte, double, float, int, long, short。
Number源码
package java.lang;
import java.io.Serializable;
/**
* 数值类型包装类的共同祖先,声明了各种包装类型的拆箱方法
*/
public abstract class Number implements Serializable {
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -8742448824652078965L;
/**
* 以byte形式返回当前对象的值
*/
public byte byteValue() {
return (byte) intValue();
}
/**
* 以short形式返回当前对象的值
*/
public short shortValue() {
return (short) intValue();
}
/**
* 以int形式返回当前对象的值
*/
public abstract int intValue();
/**
* 以long形式返回当前对象的值
*/
public abstract long longValue();
/**
* 以float形式返回当前对象的值
*/
public abstract float floatValue();
/**
* 以double形式返回当前对象的值
*/
public abstract double doubleValue();
}
Number源码测试
package testJdk.lang;
import org.junit.Test;/** * @author tomatocc * @email 553866242@qq.com * @desc */public class NumberTest { @Test public void numberTest(){ Integer num = new Integer(12); System.out.println("intValue(): " + num.intValue()); System.out.println("longValue(): " + num.longValue()); System.out.println("floatValue(): " + num.floatValue()); System.out.println("doubleValue(): " + num.doubleValue()); System.out.println("byteValue(): " + num.byteValue()); System.out.println("shortValue(): " + num.shortValue()); }}
运行结果如下:
intValue(): 12
longValue(): 12floatValue(): 12.0doubleValue(): 12.0byteValue(): 12shortValue(): 12

长
按
关
注
解锁更多精彩内容
