
? 概念:根据八种基本数据类型相应的引用数据类型
八种基本类型数据 | 包装类 | 父类 |
---|---|---|
int | Integer | Numb |
short | Short | Numb |
long | Long | Numb |
double | Double | Numb |
float | Float | Numb |
char | Character | Object |
boolean | Boolean | Object |
?1 数据的装箱和拆箱
(1)装箱:基本类型=>引用类型
(2)拆箱:引用类型=>基本类型
在jdk5以前装箱和拆箱操作必须,手动进行;在jdk5(包括5)以后会自动装箱拆箱
手动装、拆箱,代码示例:
public class Test {
public static void main(String[] args) {
// 手动装箱过程
int num = 100,temp;
// 方式①
Integer integer = new Integer(num);
Integer integer1 = Integer.valueOf(num);
// 手动拆箱
temp = integer.intValue();
}
}
自动装、拆箱代码示例
public class Test {
public static void main(String[] args) {
// 自动装箱过程
int num = 100,temp;
Integer integer = num;
// 手动拆箱
temp = integer;
}
}
?2 包装类和String之间的转换
?2.1示例代码:
public class Test {
public static void main(String[] args) {
// String => 包装类
String str = "123",strTemp;
// 方法①
Integer integer = Integer.getInteger(str);
// 方法②
integer = new Integer(str);
// 包装类=>String
// 方法①
strTemp = integer.toString();
// 方法②
strTemp = integer+"";
// 方法③
strTemp = String.valueOf(integer);
}
}
结语
大佬请留步
既然看到这了不如点个赞?再走吧
本文目的在于分享技术以及在学习过程中个人记得需要注意的点,记录学习过程;
如果出现错误欢迎大家指正,如有意见或建议欢迎在评论区讨论
本文含有隐藏内容,请 开通VIP 后查看