基本类型、包装类与String类间的转换
前言
包装类的本质就是将基本数据类型以及一些辅助方法包装到类中,即将基本数据类型“包装起来”,使得它具有了对象的性质,并且为其添加了属性和方法吗,丰富了基本类型的操作。
基本数据类型 | 对应的包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
一、装箱和拆箱
- 装箱:包装类使得一个基本数据类型的数据变成了类。有了类的特点,可以调用类中的方法。
举例:
@Test
public void test6(){
int i = 500;
Integer t = new Integer(i);//基本数据类型转为包装类
String s = t.toString(); // s = “500“,t是类,有toString方法
System.out.println(s);//包装类型转为基本数据类型
}
运行结果:
500
Process finished with exit code 0
- 拆箱:将数字包装类中内容变为基本数据类型。
举例:
@Test
public void test6(){
int i = 500;
Integer t = new Integer(i);
String s = t.toString(); // s = “500“,t是类,有toString方法
int j = t.intValue(); // j = 500,intValue取出包装类中的数据
System.out.println(j);
}
运行结果:
500
Process finished with exit code 0
二、基本类型、包装类与String类间的转换
通过图片了解三者之间的关系:
1.包装类—>基本数据类型:
格式:
调用包装类Xxx.xxxValue()
举例:
@Test
public void test2(){
Integer in1 = new Integer(12);//装箱
//包装类--->基本数据类型
int i1 = in1.intValue();
System.out.println(i1 + 1);
//包装类--->基本数据类型
Float f1 = new Float(12.3);//装箱
float f2 = f1.floatValue();
System.out.println(f2 + 1);
}
2.基本数据类型、包装类—>String类型
格式:
调用String重载的valueOf(Xxx xxx)
举例:
@Test
public void test4(){
int num1 = 10;
//方式1:连接运算
String str1 = num1 + "";
//方式2:调用String的valueOf(Xxx xxx)
float f1 = 12.3f;//基本数据类型
String str2 = String.valueOf(f1);//"12.3"
Double d1 = new Double(12.4);//包装类
String str3 = String.valueOf(d1);
System.out.println(str2);
System.out.println(str3);//"12.4"
}
3.String类型 —>基本数据类型、包装类
格式:
调用包装类的parseXxx(String s)
举例:
@Test
public void test5(){
String str1 = "123";
//错误的情况:
// int num1 = (int)str1;
// Integer in1 = (Integer)str1;
//可能会报NumberFormatException
int num2 = Integer.parseInt(str1);
System.out.println(num2 + 1);
String str2 = "true1";
boolean b1 = Boolean.parseBoolean(str2);
System.out.println(b1);
}
4.String 与 char[]之间的转换
格式:
String --> char[]:调用String的toCharArray()
char[] --> String:调用String的构造器
举例说明:
@Test
public void test2(){
String str1 = "abc123"; //题目: a21cb3
//String --> char[]:调用String的toCharArray()
char[] charArray = str1.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.println(charArray[i]);
}
// char[] --> String:调用String的构造器
char[] arr = new char[]{'h','e','l','l','o'};
String str2 = new String(arr);
System.out.println(str2);
}
5.String 与 byte[]之间的转换
格式:
编码:String --> byte[]:调用String的getBytes()
解码:byte[] --> String:调用String的构造器
编码:字符串 -->字节 (看得懂 --->看不懂的二进制数据)
解码:编码的逆过程,字节 --> 字符串 (看不懂的二进制数据 ---> 看得懂)
说明:解码时,要求解码使用的字符集必须与编码时使用的字符集一致,否则会出现乱码。
举例说明:
@Test
public void test3() throws UnsupportedEncodingException {
String str1 = "abc123中国";
byte[] bytes = str1.getBytes();//使用默认的字符集,进行编码。
System.out.println(Arrays.toString(bytes));
byte[] gbks = str1.getBytes("gbk");//使用gbk字符集进行编码。
System.out.println(Arrays.toString(gbks));
System.out.println("******************");
String str2 = new String(bytes);//使用默认的字符集,进行解码。
System.out.println(str2);
String str3 = new String(gbks);
System.out.println(str3);//出现乱码。原因:编码集和解码集不一致!
String str4 = new String(gbks, "gbk");
System.out.println(str4);//没有出现乱码。原因:编码集和解码集一致!
}