//装箱
//将基本类型转化为引用类型
int a = 2;
Integer p1 = new Integer(a);
System.out.println(p1);
//拆箱
//将引用类型转化为基本类型
int p2 = p1.intValue();
System.out.println(p2);
//将字符串转化为其他类型
String s = "123";
int s1 = Integer.parseInt(s);
double s2 = Double.parseDouble(s);
float s3 = Float.parseFloat(s);
//将其他类型转化为字符串
//先将其包装成对象,然后调用对象中的方法
Integer number = new Integer(234);
String s4 = number.toString();
System.out.println(s4);
Java学习笔记:装箱,拆箱,字符串转化为其他类型 以及 其他类型转化为字符串
这篇博客介绍了Java中基本类型与引用类型之间的转换,包括装箱(int到Integer)和拆箱(Integer到int)操作,以及如何将字符串转化为数值类型(Integer.parseInt, Double.parseDouble, Float.parseFloat)和将数值类型转化为字符串(toString方法)。内容涵盖了Java类型转换的基础知识。

被折叠的 条评论
为什么被折叠?



