07Java常用类2之包装类

包装类

定义

包装类的作用:基本类型不是类,所以无法获得类的基本特性,无法参与转型、泛型、集合、反射等过程,包装类的出现就是为了弥补这个缺陷。

为了对基本类型的数据进行更多的操作,将 基本数据类型 封装成类,并在类中定义一些功能,这个类就是 包装类(8个)

  byte Byte

  short Short

  int Integer

  long Long

  float Float

  double Double

  char Character

  boolean Boolean

常见的操作之一:用于基本数据类型与字符串之间的转换。

装箱与拆箱

装箱

​ 将 基本数据类型 转换为 包装类类型。

​ 自动装箱 基本类型 – 包装类类型(引用类型)

拆箱

​ 将 包装类类型 转换为 基本数据类型。

​ 自动拆箱 包装类类型(引用类型)– 基本类型

自动装箱与拆箱

从JDK1.5开始可以自动装箱与拆箱。所以下面是正确的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        Integer num1 = 100;
        int num2 = new Integer(100);

      把下面的这个代码理解即可:

        Integer num = 100;
        num += 200;
        System.out.println("num:" + num);

      通过*反编译*后的代码:

        Integer num = Integer.valueOf(100); // 自动装箱
        num = Integer.valueOf(num.intValue() + 200); // 自动拆箱,再自动装箱
        System.out.println((new StringBuilder("num:")).append(num).toString()); // 我们用 + 号做的拼接,其本质是调用append()方法。因为用 + 号做拼接内存占用空间比较大,字符串拼接浪费很多空间,所以在底层自动做了转换。

      需要注意:
        在使用时,Integer num = null;上面的代码就会出现NullPointerException。因为num为空引用无法调用方法。

Integer

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

  此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。

构造方法

1
2
3
4
5
6
7
8
9
10
11
         public Integer(int value)

        Integer i = new Integer(100);

     public Integer(String s)

        Integer i = new Integer("100");

      注意:这里的字符串必须是由数字字符组成

          抛出: NumberFormatException - 如果 String 不包含可解析的整数。

成员方法

1
2
3
4
5
public static final int MAX_VALUE
System.out.println(Integer.MAX_VALUE);// 2147483647

public static final int MIN_VALUE
System.out.println(Integer.MIN_VALUE);// -2147483648

​ A. String、包装类、基本数据类型 的相互转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        public int intValue()
        public static int parseInt(String s)
        public static String toString(int i)
        public static Integer valueOf(int i)
        public static Integer valueOf(String s)

       String – 包装类 //将String转换为包装类,使用包装类的valueOf()
          String s = "100";
          Integer num1 = Integer.valueOf(s);
          Double num2 = Double.valueOf(s);

       String – int //将String转换为基本数据类型,使用对应包装类的parseXXX()即可
          String str = "100";
        1). int num1 = new Integer(str).intValue();
        2). int num2 = Integer.parseInt(str); //推荐,*记住很重要*
        3). double num3 = Double.parseDouble(str)

       int – String
          int value = 100;
        1). String s1 = "" + value;
        2). String s2 = String.valueOf(value); //推荐,它可以将任意类型转换成字符串
        3). String s3 = new Integer(value).toString();
        4). String s4 = Integer.valueOf(value).toString();
        5). String s5 = Integer.toString(value);

​ B. 进制转换(了解)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        public static String toBinaryString(int i)
        public static String toOctalString(int i)
        public static String toHexString(int i)
        public static String toString(int i, int radix)
        public static int parseInt(String s, int radix)

       常用的基本进制转换:
        public static String toBinaryString(int i)
          System.out.println(Integer.toBinaryString(100));// 1100100
        public static String toOctalString(int i)
          System.out.println(Integer.toOctalString(100));// 144
        public static String toHexString(int i)
          System.out.println(Integer.toHexString(100));// 64

       十进制转其他进制:
        public static String toString(int i, int radix) :radix的范围(即进制的范围)236
        为什么? 0,...9,a...z 一共36
          System.out.println(Integer.toString(100, 2).toUpperCase());// 1100100
          System.out.println(Integer.toString(100, 8).toUpperCase());// 144
          System.out.println(Integer.toString(100, 16).toUpperCase());// 64
          System.out.println(Integer.toString(100, 1).toUpperCase());// 100
          System.out.println(Integer.toString(100, 37).toUpperCase());// 100

       其他进制转十进制:
        public static int parseInt(String s, int radix)
        System.out.println(Integer.parseInt("1100100", 2));// 100
        System.out.println(Integer.parseInt("144", 8));// 100
        System.out.println(Integer.parseInt("64", 16));// 100

        System.out.println(Integer.parseInt("100", 10));// 100
        System.out.println(Integer.parseInt("100", 2));// 4
        System.out.println(Integer.parseInt("100", 8));// 64
        System.out.println(Integer.parseInt("100", 16));// 256

        System.out.println(Integer.parseInt("123", 2)); //抛出:NumberFormatException - 如果 String 不包含可解析的 int。

案例

​ -128到127之间的数据缓冲池问题

​ 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class IntegerDemo {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2); // false
System.out.println(i1.equals(i2)); // true
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4); // false
System.out.println(i3.equals(i4)); // true
Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6); // false
System.out.println(i5.equals(i6)); // true
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8); // true
System.out.println(i7.equals(i8)); // true
// 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
// Integer ii = Integer.valueOf(127);
}
}

Character(了解)

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

​ 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然。

​ 字符信息基于 Unicode 标准,版本 4.0。

构造方法

1
2
3
4
public Character(char value)
Character ch = new Character((char) 97);
Character ch = new Character('a');
System.out.println("ch:" + ch);//ch:a -- 说明重写了toString()方法。

成员方法

1
2
3
4
5
A.    public static boolean isUpperCase(char ch):    判断给定的字符是否是大写
B. public static boolean isLowerCase(char ch): 判断给定的字符是否是小写
C. public static boolean isDigit(char ch): 判断给定的字符是否是数字字符
D. public static char toUpperCase(char ch): 把给定的字符转换为大写
E. public static char toLowerCase(char ch): 把给定的字符转换为小写

案例

​ 统计字符串中大写,小写及数字字符出现的次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class IntegerDemo {
public static void main(String[] args) {
// 定义三个统计变量。
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
// 键盘录入一个字符串。
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
// 把字符串转换为字符数组。
char[] chs = line.toCharArray();
// 历字符数组获取到每一个字符
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];
// 判断该字符
if (Character.isUpperCase(ch)) {
bigCount++;
} else if (Character.isLowerCase(ch)) {
smallCount++;
} else if (Character.isDigit(ch)) {
numberCount++;
}
}
// 输出结果即可
System.out.println("大写字母:" + bigCount + "个");
System.out.println("小写字母:" + smallCount + "个");
System.out.println("数字字符:" + numberCount + "个");
}
}

弊端

包装类的弊端,不断地拆装箱会引发性能问题:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Test {
public static void main(String[] args) {
Integer sum1 = 1;

StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int i = 0; i < 1000000000; i++) {
sum1 += 1; // Integer.valueOf(sum1.intValue() + i);
}
stopWatch.stop();
System.out.println(stopWatch.getTotal(TimeUnit.MILLISECONDS)); // 当 sum1 为int类型时,只需要1毫秒
System.out.println(sum1);
}
}

重载和自动拆装箱

有更匹配的重载方法时,不自动拆装箱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Test {
public static void main(String[] args) {
fun(new Integer(3)); // Param is a Integer!
fun(3); // Param is a int!
}

static void fun(Integer i) {
System.out.println("Param is a Integer!");
}

static void fun(int i) {
System.out.println("Param is a int!");
}
}

常量池

注意:包装类的比较必须使用equals(), ‘==’ 比较的是两个引用是否指向一个对象。

包装类的创建很浪费性能,因此Java对简单的数字(-128~127)对应的包装类进行了缓存,称为常量池。通过直接量赋值的包装类如果在此范围内,会直接使用常量池中的引用。

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test {
public static void main(String[] args) {
Integer i1 = 200;
Integer i2 = 200;
Integer i3 = 100;
Integer i4 = 100;
Integer i5 = new Integer(100);

System.out.println(i1 == i2); // false
System.out.println(i3 == i4); // true
System.out.println(i3 == i5); // false
}
}