Java基础(八)对象包装器与自动装箱

2023-05-03,,

  1.对象包装器

  有时候,需要将int这样的基本类型转换为对象。所有的基本类型都有一个与之对应的类。通常,这些类被称为包装器(wrapper)。

  这些对象包装类分别是:Integer、Long、Float、Double、Short、Byte、Character、Void和Boolean。

  对象包装类是不可变的,即一旦构造了包装器,就不允许更改包装在其中的值。同时,对象包装器类还是final,因此不能定义它们的子类。

  例如:如果想定义已给整型数组列表,但是尖括号中的类型参数不允许是基本类型,即不允许写成ArrayList<int>,这时,需要写成:

ArrayList<Integer> list = new ArrayList();

  由于每个值分别包装在对象中,所以ArrayList<Integer>的效率远远低于int[] 数组。因此,可以用来构造小型数组,因为此时操作方便性比执行效率更加重要。

  

  2.自动装箱与自动拆箱

  如果这时调用:

list.add(3);

  编译器会把这条语句自动变成:

list.add(Integer.valueOf(3));

  这种变换被称为自动装箱。(autoboxing)

  对应的,当将一个Integer对象赋值给一个int值时,将会自动地拆箱:

  如果这时调用:

int n = list.get(i);

  编译器会把这条语句自动变成:

int n = list.get(i).intValue();

  在算术表达式中也能够自动装箱和拆箱,例如:编译器会自动插入一条对象拆箱的指令,然后进行自增计算,最后再将结果装箱。

Integer n = 3;
n++;

  

  3.对象包装器对象的比较

  使用“==”比较两个对象包装器对象时,检测的是对象是否指向同一个存储区域。因此,下面的比较通常不会成立:

Integer a = 1000;
Integer b = 1000;
if (a == b) ...;

  因此,对两个对象包装器的比较需要调用equals方法。

  4.自动装箱的空指针异常

  例如:对象包装类对象可以是null,因此,在自动拆箱后,下面的代码将抛出空指针异常

Integer n = null;
System.out.print(2*n);

  5.混合对象包装器在自动拆箱时自动装换类型

  例如:执行过程是,Integer类型的值n将自动拆箱,然后将int提升为double,最后再装箱成Double

Integer n = 1;
Double x = 2.9;
if (n == x)...;

  6.对象包装器类的一些基本方法

  对象包装类包括:Integer、Long、Float、Double、Short、Byte、Character、Void和Boolean。

  其中,Integer、Long、Float、Double、Short、Byte这六个类是Number类的子类。

  (1)Integer类(代表Integer、Long、Short类)

  Integer类在对象中包装了一个基本类型int的值,该类的对象包含一个int类型的字段。

构造方法

1.Integer(int number):以一个int变量为参数来获取Integer对象
Integer number = new Integer(7);
2.Integer(String str):以一个String变量为参数来获取Integer对象
Integer number = new Integer("77");

常用方法

  

  

常量

public static final int   MIN_VALUE = 0x80000000;  // 表示int类型可取到的最大值,即2^31-1
public static final int MAX_VALUE = 0x7fffffff; // 表示int类型可取到的最小值,即-2^31
public static final int SIZE = 32; // 表示以二进制补码形式表示int类型的值的位数
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); // 表示基本类型int的Class实例

示例代码

package integer.jun.iplab;

public class IntegerTest {

    public static void main(String[] args) {

        int type_int = Integer.parseInt("777");
System.out.println(type_int);
int type_int_sum = Integer.parseInt("233") + Integer.parseInt("777");
System.out.println(type_int_sum); System.out.println("十进制字符串表示: " + Integer.toString(456));
System.out.println("二进制字符串表示: " + Integer.toBinaryString(456));
System.out.println("八进制字符串表示: " + Integer.toOctalString(456));
System.out.println("十六进制字符串表示: " + Integer.toHexString(456));
}
} 输出:
777
1010
十进制字符串表示: 456
二进制字符串表示: 111001000
八进制字符串表示: 710
十六进制字符串表示: 1c8

  (2)Boolean类

  Boolean类将基本类型为boolean的值包装在一个对象中,一个Boolean类型的对象只包含一个类型为boolean的字段。

构造方法

1.Boolean(boolean value):以一个int变量为参数来获取Boolean对象
Boolean b = new Boolean (true);
2.Boolean(String str):以一个String变量为参数来获取Boolean对象
Boolean b = new Boolean ("ok");

常用方法

  

常量

public static final Boolean FALSE = new Boolean(false);  // 对应基值为false的Boolean对象
public static final Boolean TRUE = new Boolean(true); // 对应基值为true的Boolean对象
public static final Class<Boolean> TYPE = (Class<Boolean>) Class.getPrimitiveClass("boolean"); // 基本类型为boolean的Class对象

示例代码

package integer.jun.iplab;

public class BooleanTest {

    public static void main(String[] args) {

        Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("Ok");
System.out.println(b1);
System.out.println(b2);
System.out.println(b1.booleanValue());
System.out.println(b2.booleanValue());
System.out.println(b1.toString());
}
}
输出:
true
false
true
false
true

  (3)Byte类

  Byte类将基本类型为byte的值包装在一个对象中,一个Byte类型的对象只包含一个类型为byte的字段。

构造方法

1.Byte(bytevalue):以一个byte变量为参数来获取Byte对象
byte mybyte = 45;
Byte b = new Byte(mybyte);
2.Byte(String str):以一个String变量为参数来获取Byte对象
Byte b = new Byte("45");

常用方法

返回值                 方法                              功能描述
byte byteValue() 以一个byte值返回Byte对象
int compareTo(ByteanotherByte) 在数字上比较两个Byte对象
double doubleValue() 以一个double值返回此Byte的值
int intValue() 以一个int值返回此Byte的值
byte parseByte(String s) 将String类型参数解析成等价的字节(byte)形式
String toString() 返回表示此Byte值的String对象
Byte valueOf(String str) 返回表示此String对象的Byte对象
boolean equals(Object obj) 将此对象与指定对象比较,如果对象相等则返回true,否则返回false

常量

public static final byte   MAX_VALUE = 127;      // 表示byte类型可取的最大值 2^8-1,即01111111
public static final byte MIN_VALUE = -128; // 表示byte类型可取的最小值 -2^8,即原码为11111111,补码为10000001-1=10000000
public static final int SIZE = 8; // 表示以二进制补码形式表示byte值的位数

  (4)Character类

  Character类将基本类型为char的值包装在一个对象中,一个Character类型的对象只包含一个类型为char的字段。

构造方法

   // Character(char value):以一个char 变量为参数来获取Character对象
char mychar = 's';
Character c = new Character(mychar);

常用方法

  

代码示例

package integer.jun.iplab;

public class CharacterTest {

    public static void main(String[] args) {

        Character c1 = new Character('A');
Character c2 = new Character('a'); System.out.println(Character.isUpperCase(c1)); // true
System.out.println(Character.isUpperCase(c2)); // false
System.out.println(Character.isLowerCase(c1)); // false
System.out.println(Character.isLowerCase(c2)); // true System.out.println(Character.toUpperCase(c2)); // A
System.out.println(Character.toLowerCase(c1)); // a System.out.println(c1.toString()); // A
System.out.println(c1.charValue()); // A
}
}

  (5)Double类(代表Double、Float类)

  Double类将基本类型为double的值包装在一个对象中,一个Double类型的对象只包含一个类型为double的字段。

构造方法

1.Double(double value):以一个byte变量为参数来获取Double对象2.Double(String str):以一个String变量为参数来获取Double对象

常用方法

  

  (6)Number类

  Integer、Long、Float、Double、Short、Byte这六个类是Number类的子类。

  Number的子类必须提供将表示的数值转换为六种基本数据类型的方法,例如在Number类的所有子类都包含下面的六中方法。

  

Java基础(八)对象包装器与自动装箱的相关教程结束。

《Java基础(八)对象包装器与自动装箱.doc》

下载本文的Word格式文档,以方便收藏与打印。