Java 简明教程
Java - Autoboxing and Unboxing
Java Autoboxing
程序自动打包是 Java compiler 用来将原始值自动转换为其 wrapper 对应值的技术。例如,当将 int 值分配给 Integer 封装对象时,编译器会自动将 int 值转换为对象,而无需显式强制转换 int 值或通过调用任何 method 将 int 转换为 Integer 对象。自动打包也称为打包。
Autoboxing is a technique used by Java compiler to automatically convert a primitive value to its wrapper counterpart. For example, when an int value is assigned to an Integer wrapper object, compiler automatically converts the int value to the object without any need to explicitly cast the int value or by calling any method to convert int to Integer object. Autoboxing is also known as boxing.
// Autoboxing
Integer obj = 10;
// Explicit casting
Integer obj2 = Integer.valueOf(10)
在这两种情况下,封装对象都使用 int 值进行初始化。在第一种情况下,自动装箱起作用,而在第二种情况下,我们明确地将 int 值强制转换成 Integer 封装对象。
In both cases, the wrapper objects are initialized with an int value. In first case, autoboxing is playing role and second case, we’re explicitly casting int value to Integer wrapper object.
编译器在以下情况下使用自动装箱:
Compiler uses autoboxing in following scenarios −
-
If a primitive value is passed as an argument to a function which is expecting a wrapper class object.
-
If a primitive value is assigned to a variable of the type of wrapper class.
Example of Autoboxing in Java
在此示例中,我们创建了一个 Integer 列表,因为 List 只能包含对象。现在,在向此列表添加项时,我们不会创建任何 Integer 对象,但我们只是传递原始 int 值。Java 编译器会自动处理转换,因此程序会编译成功。我们使用了另一个将 char 原始值分配给 Character 对象的案例,它也能正常工作。
In this example, we’ve created a list of Integer as List can contain only objects. Now while adding the item to this list, we’re not creating any Integer object but we’re just passing the primitive int values. Java compiler automatically handles the conversion and program compiles successfully. We’ve used another case of assigning a char primitive value to Character object which works as well.
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
public class Tester {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for(int i = 0; i< 10; i++){
// autoboxing by passing as an argument
// int value is converted to Integer
// by compiler during compilation
list.add(i);
}
System.out.println(list);
char c = 'a';
//autoboxing by assigning a char to Character object
Character ch = c;
System.out.println(ch);
}
}
让我们编译并运行上述程序,而不进行任何 command line argument,这将产生以下结果 −
Let us compile and run the above program without any command line argument, this will produce the following result −
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a
Java Unboxing
解包是自动打包的逆过程。Java 编译器使用解包将封装对象转换为其原始对应的对象。例如,当将 Integer 对象作为参数传递给方法时,而所调用的方法期望有 int variable 时,编译器会自动将 Integer 对象转换为 int 值,然后将它传递给所调用的方法。同样,如果将封装值分配给原始变量,Java 编译器也会解包封装值。因此我们不需要从封装对象中显式获取 int 值。
Unboxing is reverse of autoboxing. Unboxing is used by Java compiler to convert a wrapper object to its primitive counterpart. For example when an Integer object is passed to a method as argument but the method called is expecting an int variable, then compiler automatically converts the Integer object to int value and then pass it to the mathod called. Similarly Java compilier unboxes the wrapper value if it is assigned to a primitive variable. Thus we are not required to explicitly get the int value from the wrapper object.
Integer obj = Integer.valueOf(10);
// Unboxing
int i = obj;
// Explicit value deduction
i = obj.intValue();
在这两种情况下,基本类型值都使用 int 值进行初始化。在第一种情况下,拆箱起作用,而在第二种情况下,我们明确地从 Integer 封装对象中获取 int 值。
In both cases, the primitive values are initialized with an int value. In first case, unboxing is playing role and second case, we’re explicitly getting int value from an Integer wrapper object.
编译器在以下情况下使用拆箱:
Compiler uses unboxing in following scenarios −
-
If a wrapper class object is passed as an argument to a function which is expecting a primitive value.
-
If a wrapper class object is assigned to a variable of primitive type.
Example of Unboxing in Java
在此示例中,我们创建了一个 Integer 对象,并用一个值 10 对它进行初始化。此对象被传递给 abs() 方法,该方法需要 int,即基本类型变量。Java 编译器会自动处理转换,并且程序可以成功编译。我们使用了将 Integer 对象分配给 int 变量的另一种情况,这也同样有效。
In this example, we’ve created an Integer object and initilized it with a value of 10. This object is passed to abs() method which is expecting an int, a primitive variable. Java compiler automatically handles the conversion and program compiles successfully. We’ve used another case of assigning a Integer object to int variable which works as well.
package com.tutorialspoint;
public class Tester {
public static void main(String[] args) {
Integer integer = Integer.valueOf(-10);
// unboxing by passing as an argument
// Integer object is converted to int
// by compiler during compilation
int i = abs(integer);
System.out.println(i);
//unboxing by assigning an Integer object to int variable
int j = integer;
System.out.println(j);
}
private static int abs(int i){
return (i < 0)? -i: i;
}
}
让我们编译并运行上述程序,没有任何命令行参数,这将产生以下结果:
Let us compile and run the above program without any command line argument, this will produce the following result −
10
-10
Mapping of Primitive and Wrapper Objects
Sr. No. |
Primitive |
Wrapper |
method to get value |
1 |
byte |
Byte |
byteValue() |
2 |
short |
Short |
shortValue() |
3 |
int |
Integer |
intValue() |
4 |
long |
Long |
longValue() |
5 |
float |
Float |
floatValue() |
6 |
double |
Double |
doubleValue() |
7 |
char |
Character |
charValue() |
8 |
boolean |
Boolean |
booleanValue() |