包装类
- 包装类?
- 包装类和基本数据类型
- 包装类的常用方法
回顾数据类型
- 数据类型
- 基本数据类型
- 数值
- 整数 byte short int long
- 浮点 float double
- 字符 char
- 布尔 boolean
- 数值
- 引用数据类型
- class
- interface
- array
- 基本数据类型
基本数据类型和包装类的对应关系
基本数据类型,不具备对象特征,没有对象化的属性和方法
包装类可以让基本数据类型具有了,对象特征
基本数据类型 | 对应包装类 |
---|---|
byte | Byte |
short | Shot |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
包装类的常用方法
基本数据类型和包装类之间的转换
- 装箱
- 自动装箱
- 手动装箱
- 拆箱
自动装箱
package wrap;
public class WrapTestOne {
public static void main(String[] args) {
// 装箱操作
// 1. 自动装箱
int t1 = 2;
Integer t2 = t1; // 自动将int类型的t1转换为Integer类型的t2
System.out.println(t1);
System.out.println(t2);
}
}
手动装箱
package wrap;
public class WrapTestOne {
public static void main(String[] args) {
// 装箱操作
// 2. 手动装箱
int t1 = 10;
Integer t3 = new Integer(t1);
System.out.println(t1);
System.out.println(t3);
}
}
自动拆箱
package wrap;
public class WrapTestOne {
public static void main(String[] args) {
// 拆箱操作
int t1 = 10;
Integer t3 = new Integer(t1);
// 1. 自动拆箱
int t4 = t3;
System.out.println(t1);
System.out.println(t3);
System.out.println(t4);
}
}
手动拆箱
package wrap;
public class WrapTestOne {
public static void main(String[] args) {
// 拆箱操作
int t1 = 10;
Integer t3 = new Integer(t1);
// 2. 手动拆箱
int t5 = t3.intValue();
// 可以通过包装类的方法来对数据进行转换
double t6 = t3.doubleValue();
System.out.println(t1);
System.out.println(t3);
System.out.println(t5);
System.out.println(t6);
}
}
基本数据类型和字符串之间的转换
基本数据类型转换成为字符串
package wrap;
public class WrapTestTwo {
public static void main(String[] args) {
// 将基本数据类型转为字符串
int t1 = 1000;
String t2 = Integer.toString(t1);
System.out.println(t1 + t1);
System.out.println(t2 + t2);
}
}
字符串转换成为基本数据类型
package wrap;
public class WrapTestTwo {
public static void main(String[] args) {
// 将字符串转为基本数据类型
// 方式一
int t3 = Integer.parseInt(new String("1000"));
System.out.println(t3+t3);
}
}
package wrap;
public class WrapTestTwo {
public static void main(String[] args) {
// 将基本数据类型转为字符串
// 方式二
String st = "1000";
int t1 = Integer.valueOf(st);
/*
赋值符号右侧,将String转换为Integer对象
赋值符号左侧,Integer对象执行自动拆箱的操作,赋值给t1[int]
*/
System.out.println(t1 + t1);
}
}
小练习:
package wrap;
public class WrapTest {
public static void main(String[] args) {
// 使用两个new关键字来实现Integer对象的结果
Integer one = new Integer(100);
Integer two = new Integer(100);
System.out.println(one==two);
}
}
结果为false
因为==
用来判断两个对象是否相等的时候,判断的是内存地址,new 实现了
两个不同的内存地址.
package wrap;
public class WrapTest {
public static void main(String[] args) {
int s = 100;
Integer three = s;
Integer four = s;
System.out.println(three==four); //true
System.out.println(three==100); // true 装箱后拆箱,还是同一个
System.out.println(100==four); // true
}
}
通过同一个Int的自动装箱的方式,实现两个Integer对象后用==
比较的结果是true
,
这是应为,当执行Integer three/four = s;
的时候,编译器的执行过程为:
1. Integer three = Integer.valueOf(s);
如果 -128<s<127, 范围内,那么就会创建对象池
2. Integer four = s;的时候会将上面对象池中的对象指向four
3. 那么three和four指向的是同一个对象
package wrap;
public class WrapTest {
public static void main(String[] args) {
// 超出范围后
Integer five = 200;
Integer six = 200;
System.out.println(five==six); //false
}
}
超出(-128,127)范围后会实例新的对象,而不是取用对象池中的对象
上述的概念就是对象常量池
但是存在特例:float,double
是不使用对象常量池的