一、基本数据类型各自对应的包装类
1. byte --- Byte
2 . short -- Short
3. int --- Integer
4. long --- Long
5. float --- Float
6. double ---Double
7. char --- Character
8 boolean --- Boolean
二、应用
- 封箱 : Integer in = new Integer(123); //强一个基本数据类型的值转换为对应的引用类型的对象。System.out.println(in);//打印出的是对象的值,而不是地址 --- 底层对toString做过重写
- 字符串转Integer : Integer in = new Integer("0x23"); //字符串底层是以字符串数组形式存储,在转换时会一次判断每一位是不是数字,所以只支持十进制的数。
- 数值型的基本数据类型的包装类都是Number类的子类。
- Boolean b = new Boolean(str); //要求参数是字符串,当str="true"时 b值为 true;当str为"false"或任意字符串时,b值为false。
- 自动封箱: int oi = 7; Integer in = i; System.out.println(in); //将一个基本数据类型的变量直接赋值给对应引用类型。是JDK1.5的新特性 之一。底层实现:自动封箱实际上是调用了valueOf() --> Integer in = Integer.valueOf(i); //valueOf返回一个Integer类型的实例。
- 自动拆箱:Integer in = new Integer(123); int i = in; System.out.println(i); //将一个引用类型的实例直接赋值给对应基本数据类型的变量。 JDK1.5的新特性之一。底层实现:实际上是调用了****value()方法。---> int i = in.intVlaue(); // ***value() 返回对应基本类型的值。
- 整数的哈希码是自己本身。Integer底层对hashCode()方法做过从写。
- 数值类型的哈希码是不变的。
- ****: Integer i1 = 123; Integer i2 = 123; i1==i2 ;//true;Integer i3 = 1234; Integer i4 = 1234; i3==i4;//false 解释:int类型的值在常量池中初始化,只初始化-128~127的范围。所以当值在-128~127中时,是直接从从常量池中取值,而常量池中的常量是被共享的,所以i1==i2;当值 不在-128~127之间时,会new Integer(),所以i3、i4指向的堆内存地址 不一样。
- 自动拆箱应用: Integer i1 = new Ingeter(123); int i = 123; i1 == i; //true 解释: 当引用类型与基本类型直接运算时,引用类型会自动拆箱。