Java包装类(Wrapper Class)
我们知道java基本数据类型有int、float、double、boolean、char 等。基本数据类型是不具备对象的特性的,比如基本类型不能调用方法、功能简单。为了让基本数据类型也具备对象的特性, Java 为每个基本数据类型都提供了一个包装类,这样我们就可以像操作对象那样来操作基本数据类型。
基本类型和包装类之间的对应关系:
基本类型 | 对应的包装类 |
int | Integer |
byte | Byte |
short | Short |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
包装类主要提供了两大类方法:
1. 将本类型和其他基本类型进行转换的方法。
2. 将字符串和本类型及包装类互相转换的方法。
包装类的继承关系
除了Boolean、Character类型直接继承object类,其他:Byte、Short、Integer、Long、Float、Double都是继承Object的子类Number。
装箱与拆箱
基本类型与对应的包装类对象之间,来回转换的过程称为「装箱」与「拆箱」。