1.数据类型基本简介
一般情况下我们会使用数据的基本数据类型:byte、int、short、long、double、float、boolean、char;
对应的包装类型也有八种:Byte、Integer、Short、Long、Double、Float、Character、Boolean;
包装类型都是用final声明了,不可以被继承重写;
在实际情况中编译器会自动的将基本数据类型装箱成对象类型,或者将对象类型拆箱成基本数据类型;如下:
public static void main(String[] args) {
int num1 = 1;
//将基本数据类型装箱成对象包装类型
Integer num2 = num1;
Integer num3 = 3;
//将对象数据类拆箱
int num4 = num3;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2.Number类是java.lang包下的一个抽象类,提供了将包装类型拆箱成基本类型的方法,所有基本类型的包装类型都继承了该抽象类,并且是final声明不可继承改变;
package java.lang;
public abstract class Number implements java.io.Serializable {
public abstract int intValue();
public abstract long longValue();
public abstract float floatValue();
public abstract double doubleValue();
public byte byteValue() {
return (byte)intValue();
}
public short shortValue() {
return (short)intValue();
}
private static final long serialVersionUID = -8742448824652078965L;
}
转载于:https://mp.csdn.net/postedit