包装类
- 针对八种基本数据类型定义相应的引用类型——包装类(封装类)
- 有了类的特点,就可以调用类中的方法
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Bealean |
char | Character |
基本数据类型、包装类、String三者之间的相互转换
- 基本数据类型 --> 包装类:调用包装类的构造器
Integer i = new Integer(1);
- 包装类 --> 基本数据类型:调用xxxValue()
int i1 = i.intValue();
- 自动装箱:基本数据类型 --> 包装类
int num = 10;
Integer in1 = num;
- 自动拆箱:包装类 --> 基本数据类型
int num1 = in1;
- 基本数据类型包装类与String的相互转化
基本数据类型–>String
- 方式一:连接运算
int num = 20; String str1 = num + "";
- 方式二:调用String重载的valueOf(xxx xxx)
float f1 = 12.3f; String str2 = String.valueOf(f1);
String–>基本数据类型
- 调用包装类的parseXXX()
String str ="true"; boolean b1 = Boolean.parseBoolean(str);