java -- 包装类

什么是包装类

包装类可以简单理解为对基本数据类型的功能拓展,最终以封装为 class 的形式呈现。这是因为八大基本数据类型只能完成基本的算术或逻辑操作,无法实现诸如 “字符串转换” 等特殊操作。在以上背景下,包装类应运而生。

包装类分类

根据八大基本数据类型,包装类相应的可以分为八类,具体如下:

基本数据类型对应包装类
intInteger
byteByte
shortShort
longLong
charCharacter
floatFloat
doubleDouble
booleanBoolean

包装类详解(以 Integer 为例)

所在包

该类位于 java.lang 包下,使用时无需导包。

简述

先看一段 JDK 关于 Integer 的注释

1
2
3
4
5
6
7
8
9
10
/**
* The {@code Integer} class wraps a value of the primitive type
* {@code int} in an object. An object of type {@code Integer}
* contains a single field whose type is {@code int}.
*
* <p>In addition, this class provides several methods for converting
* an {@code int} to a {@code String} and a {@code String} to an
* {@code int}, as well as other constants and methods useful when
* dealing with an {@code int}.
*/

大体上是说,这是一个包含唯一类字段的类;类字段为 int 型,用来存储 int 型变量。除此之外,该类还提供了一些诸如 intString 之间相互转换的方法,用于实现一些常见操作。

构造方法

有 2 个有参构造方法,分别为:Integer(int)Integer(String)JDK 源码如下:

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
 /**
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
*/
public Integer(int value) {
this.value = value;
}

/**
* Constructs a newly allocated {@code Integer} object that
* represents the {@code int} value indicated by the
* {@code String} parameter. The string is converted to an
* {@code int} value in exactly the manner used by the
* {@code parseInt} method for radix 10.
*
* @param s the {@code String} to be converted to an
* {@code Integer}.
* @exception NumberFormatException if the {@code String} does not
* contain a parsable integer.
* @see java.lang.Integer#parseInt(java.lang.String, int)
*/
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}

首先来看 Integer(int),该方法是根据一个形参 int 实例化一个 Integer 对象,内部细节就是将形参 int 赋值给 Integer 的类字段 value

再来看 Integer(String),该方法使用一个 String 类型对象实例化 Integer。内部实现稍微复杂一点点,先是用 Integer 静态成员方法将 String 转化为对应 int 值,然后再将获得的 int 值赋值给 Integer 类字段 value

成员方法

由于在实际开发中,我们用的最多的无非就是 Stringint 的相互转化。因此,接着来我们主要总结一下这方面的成员方法。

用途静态成员方法非静态成员方法
String –> intparseInt(String s)
parseInt(String s, int radix)
int –> StringtoString(int i)
toString(int i, int radix)
String –> IntegervalueOf(String s)
valueOf(String s, int radix)
Integer –> StringtoString()
int –> IntegervalueOf(int i)
Integer –> intintValue()

另外,在阅读 Integer 源码中,发现了两个好玩的成员方法 reverse(int i)rotateLeft(int i, int distance),其中对于 bit 的操作很有启发性,现直接将源码贴在下方:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Returns the value obtained by reversing the order of the bits in the
* two's complement binary representation of the specified {@code int}
* value.
*
* @param i the value to be reversed
* @return the value obtained by reversing order of the bits in the
* specified {@code int} value.
* @since 1.5
*/
public static int reverse(int i) {
// HD, Figure 7-1
// 举个例子:对于一个字节"abcdefgh"
i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; // abcd efgh --> badc fehg
i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; // badc fehg --> dcba hgfe
i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; // dcba hgfe --> hgfe dcba
// 下面是完成4个字节的反向 1 <--> 4 以及 2 <--> 3
i = (i << 24) | ((i & 0xff00) << 8) |
((i >>> 8) & 0xff00) | (i >>> 24);
return i;
}


/**
* Returns the value obtained by rotating the two's complement binary
* representation of the specified {@code int} value left by the
* specified number of bits. (Bits shifted out of the left hand, or
* high-order, side reenter on the right, or low-order.)
*
* <p>Note that left rotation with a negative distance is equivalent to
* right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
* distance)}. Note also that rotation by any multiple of 32 is a
* no-op, so all but the last five bits of the rotation distance can be
* ignored, even if the distance is negative: {@code rotateLeft(val,
* distance) == rotateLeft(val, distance & 0x1F)}.
*
* @param i the value whose bits are to be rotated left
* @param distance the number of bit positions to rotate left
* @return the value obtained by rotating the two's complement binary
* representation of the specified {@code int} value left by the
* specified number of bits.
* @since 1.5
*/
public static int rotateLeft(int i, int distance) {
return (i << distance) | (i >>> -distance);
// i >>> -distance 意思是
// 如果 distance > 0: i >>> -distance = i >>> (32-(distance % 32))
// 如果 distance < 0: 与普通 i >>> 正数 并无二致
}
Powered By Valine
v1.5.2