目录
①static int compare(int x, int y)
②int compareTo(Integer anotherInteger)
①static int parseInt(String s)
②static Integer valueOf(int i) / valueOf(String s)
①static int max(int a, int b) / min(int a, int b)
③static String toString(int i)
在 Java 中,包装类(Wrapper Class)是为基本数据类型提供的一组对应的引用类型。Java 是一种面向对象的编程语言,但基本数据类型(如 int、double 等)并不具备对象的特性,为了能在一些需要使用对象的场景中操作基本数据类型,Java 提供了包装类。以下是关于 Java 包装类的详细介绍:
一、包装类的作用
-
对象化基本类型
将基本数据类型(如int
)包装成对象,使其能参与面向对象的操作(如存入集合、使用泛型等)。 -
提供功能方法
包装类内置了丰富的工具方法(如类型转换、进制转换、比较等)。 -
支持
null
值
包装类允许值为null
,适用于可能缺失数据的场景(如数据库字段可为空)。 -
支持泛型:泛型只能使用引用类型,不能使用基本数据类型。包装类作为引用类型,可以在泛型中使用。
二、基本类型与包装类对照表
基本类型 | 包装类 | 继承关系 |
---|---|---|
byte | Byte | Number → Object |
short | Short | Number → Object |
int | Integer | Number → Object |
long | Long | Number → Object |
float | Float | Number → Object |
double | Double | Number → Object |
char | Character | Object |
boolean | Boolean | Object |
三、包装类的常用方法
1. 基本数据类型与包装类对象之间的转换
装箱(Boxing):将基本数据类型转换为包装类对象。
拆箱(Unboxing):将包装类对象转换为基本数据类型。
在 Java 5 之前,需要手动进行装箱和拆箱操作,示例代码如下:
// 手动装箱
int num = 10;
Integer integerObj = new Integer(num); //转化为包装类
// 手动拆箱
Integer integerObj2 = new Integer(20);
int num2 = integerObj2.intValue();
Java 5 引入了自动装箱和拆箱机制,使得代码更加简洁,示例代码如下:
// 自动装箱
int num = 10;// 程序在编译的时候底层实际上的代码是:Integer x = new Integer(10);
Integer integerObj = num;
// 自动拆箱
Integer integerObj2 = 20;
int num2 = integerObj2; // 底层实际上会调用:int num2 = integerObj2.intValue();
/*Integer a = 10000;
Integer b = 10000;
System.out.println(a == b); // false(堆当中两个Integer对象,内存地址不同。)*/
2. 字符串与基本数据类型 / 包装类对象之间的转换
将字符串转换为基本数据类型:包装类提供了静态的 parseXxx 方法,用于将字符串转换为对应的基本数据类型。
String str = "123";
int num = Integer.parseInt(str);
将基本数据类型 / 包装类对象转换为字符串:可以使用 String.valueOf() 方法或包装类的 toString() 方法。
int num = 123;
String str1 = String.valueOf(num);
Integer integerObj = 123;
String str2 = integerObj.toString();
3. 比较两个包装类对象的值
包装类重写了 equals()
方法,用于比较两个对象的值是否相等。
Integer obj1 = 100;
Integer obj2 = 100;
System.out.println(obj1.equals(obj2)); // 输出: true
4.Java 包装类拆箱详解(补充说明)
拆箱是将包装类对象转换为对应的基本数据类型的过程。
Java 为所有包装类提供了 xxxValue()
方法实现拆箱操作,其中:
6 个数值型包装类(继承
Number
):byteValue()
、shortValue()
、intValue()
、longValue()
、floatValue()
、doubleValue()
非数值型包装类:
Boolean.booleanValue()
Character.charValue()
⑴手动拆箱 vs 自动拆箱
操作类型 | 代码示例 | 说明 |
---|---|---|
手动拆箱 | int a = integerObj.intValue(); | 显式调用 xxxValue() 方法 |
自动拆箱 | int a = integerObj; | 编译器隐式插入 intValue() |
⑵关键注意事项
①空指针风险
若包装类对象为 null,拆箱时抛出 NullPointerException:
Integer num = null;
int a = num; // 运行时错误:NullPointerException
②精度损失与溢出
跨类型转换时需谨慎(如 Double → int):
Double d = 123.456;
int i = d.intValue(); // 结果:123(直接截断小数,非四舍五入)
Long bigNum = 3000000000L;
int risky = bigNum.intValue(); // 溢出:结果为负数!
③缓存机制的影响
自动拆箱的 == 比较与值范围相关:
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true(缓存范围内,对象复用)
Integer c = 128;
Integer d = 128;
System.out.println(c == d); // false(超出缓存范围,新建对象)
⑶自动拆箱的应用场景
①算术运算
Integer x = 10;
Integer y = 20;
int sum = x + y; // 自动拆箱 → x.intValue() + y.intValue()
②条件判断
Boolean flag = true;
if (flag) { // 自动拆箱 → flag.booleanValue()
System.out.println("条件成立");
}
③集合遍历
List<Integer> list = Arrays.asList(1, 2, 3);
for (int num : list) { // 自动拆箱
System.out.println(num);
}
四、Integer 介绍
1.包装类的常量
/**
* 关于包装类的常量
*/
public class IntegerTest02 {
public static void main(String[] args) {
System.out.println("int类型最大值:" + Integer.MAX_VALUE);
System.out.println("int类型最小值:" + Integer.MIN_VALUE);
System.out.println("byte类型最大值:" + Byte.MAX_VALUE);
System.out.println("byte类型最小值:" + Byte.MIN_VALUE);
System.out.println(Boolean.FALSE);
System.out.println(Boolean.TRUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
}
}
运行结果:
2.Integer的构造方法
构造方法被标记过时的原因
在 Java 9 之前,开发者通常使用构造方法来创建包装类的实例,如 new Integer(10) 或 new Boolean("true")。但从 Java 9 开始,这些构造方法被标记为过时,主要原因是:
浪费资源:每次使用构造方法都会创建一个新的对象,即使对于相同的值也是如此,这可能会造成不必要的内存开销。
自动装箱机制:Java 5 引入了自动装箱机制,使得基本数据类型和包装类之间的转换更加方便,构造方法不再是必需的。
替代方案
对于基本数据类型转换为包装类,推荐使用自动装箱机制;对于字符串转换为包装类,推荐使用包装类的静态工厂方法,如 valueOf()。
public class WrapperClassConstructor {
public static void main(String[] args) {
// 过时的 Integer 构造方法示例
// Integer(int value)
// Integer intObj1 = new Integer(10); // Java 9 后不建议使用
// Integer(String s)
// Integer intObj2 = new Integer("20"); // Java 9 后不建议使用
// 替代方案
// 自动装箱
Integer intObj3 = 10;
System.out.println("自动装箱创建 Integer 对象: " + intObj3);
// 使用 valueOf 方法
Integer intObj4 = Integer.valueOf("20");
System.out.println("使用 valueOf 方法创建 Integer 对象: " + intObj4);
// 过时的 Boolean 构造方法示例
// Boolean(boolean value)
// Boolean boolObj1 = new Boolean(true); // Java 9 后不建议使用
// Boolean(String s)
// Boolean boolObj2 = new Boolean("true"); // Java 9 后不建议使用
// 替代方案
// 自动装箱
Boolean boolObj3 = true;
System.out.println("自动装箱创建 Boolean 对象: " + boolObj3);
// 使用 valueOf 方法
Boolean boolObj4 = Boolean.valueOf("true");
System.out.println("使用 valueOf 方法创建 Boolean 对象: " + boolObj4);
}
}
3.Java Integer
类常用方法详解
⑴、数值比较方法
①static int compare(int x, int y)
功能:比较两个 int
值的大小。
返回值:
-
0
:相等 -
1
:x > y
-
-1
:x < y
int result = Integer.compare(5, 3); // 1
②int compareTo(Integer anotherInteger)
功能:实现 Comparable
接口,比较两个 Integer
对象的值。
返回值:同 compare()
方法。
Integer a = 10;
Integer b = 20;
int result = a.compareTo(b); // -1
⑵数值转换方法
①static int parseInt(String s)
功能:将字符串转换为 int
值。
异常:若字符串非数字格式,抛出 NumberFormatException
。
String numStr = "123";
int num = Integer.parseInt(numStr); // 123
// 错误示例
try {
int invalid = Integer.parseInt("12a3"); // 抛出异常
} catch (NumberFormatException e) {
System.out.println("格式错误!");
}
②static Integer valueOf(int i)
/ valueOf(String s)
功能:将 int
或字符串转换为 Integer
对象。
特性:
-
使用缓存机制:数值在
-128
到127
之间时,返回缓存对象。 -
字符串参数需为有效数字格式。
Integer a = Integer.valueOf(100); // 使用缓存
Integer b = Integer.valueOf("200"); // 新建对象
System.out.println(a == Integer.valueOf(100)); // true(缓存范围内)
System.out.println(b == Integer.valueOf(200)); // false(超出缓存)
⑶实用工具方法
①static int max(int a, int b)
/ min(int a, int b)
功能:返回两个值的最大或最小值。
int max = Integer.max(5, 8); // 8
int min = Integer.min(5, 8); // 5
② 进制转换方法
方法:
toBinaryString(int i)
:十进制 → 二进制
toHexString(int i)
:十进制 → 十六进制
toOctalString(int i)
:十进制 → 八进制
String binary = Integer.toBinaryString(10); // "1010"
String hex = Integer.toHexString(255); // "ff"
String octal = Integer.toOctalString(8); // "10"
③static String toString(int i)
功能:将 int
转换为字符串。等价于 String.valueOf(int)
。
String str = Integer.toString(123); // "123"
⑷对象操作与拆箱
①int intValue()
功能:将 Integer
对象拆箱为 int
。
Integer num = 456;
int primitiveNum = num.intValue(); // 456
② boolean equals(Object obj)
功能:重写 Object.equals()
,比较两个 Integer
对象的值是否相等。
Integer x = 100;
Integer y = 100;
Integer z = new Integer(100);
System.out.println(x.equals(y)); // true
System.out.println(x.equals(z)); // true
五、关键注意事项
-
对象比较必须用
equals
包装类是对象,==
比较的是内存地址,而非值:Integer x = 128; Integer y = 128; System.out.println(x == y); // false(超出缓存范围) System.out.println(x.equals(y)); // true
-
空指针风险
自动拆箱时若对象为null
,会抛出NullPointerException
:Integer num = null; int value = num; // 运行时抛出 NPE!
3.String、int、Integer三种类型之间的互相转换(String,double,Double转换原理相同)
/**
* String,int,Integer三种类型之间的转换。
*/
public class IntegerTest04 {
public static void main(String[] args) {
// String ---> int
String s1 = "123";
int i1 = Integer.parseInt(s1);
System.out.println(i1 + 1);
// int ---> String
// 第一种
int i2 = 123;
String s2 = i2 + "";
System.out.println(s2 + 1); // "1231"
// 第二种
String s3 = Integer.toString(i2);
System.out.println(s3 + 1); // "1231"
// String --> Integer
String s4 = "123";
Integer i3 = Integer.valueOf(s4);
// Integer --> String
String s5 = String.valueOf(i3);
// int --> Integer
int i4 = 100;
Integer i5 = Integer.valueOf(i4);
// Integer --> int
int i6 = i5.intValue();
}
}
4.面试题
/**
* 面试题
*/
public class IntegerTest06 {
public static void main(String[] args) {
Integer x = 10000; // Integer x = new Integer(10000);
Integer y = 10000; // Integer x = new Integer(10000);
System.out.println(x == y); // false
// 整数型常量池。
// [-128 ~ 127] 这些数字太常用了。
// 为了提高效率,Java提供了一个整数型常量池。
// 这个常量池是一个数组:Integer[] integerCache;
// 这个数组中存储了256个Integer的引用。
// 只要没有超出这个范围的数字,直接从整数型常量池中取。
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true
}
}