Wrapper Classes · 包装类
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| wrapper class/ˈræpə klæs/ | 包装类 | bāo zhuāng lèi |
| autoboxing/ˌɔːtəʊˈbɒksɪŋ/ | 自动装箱 | zì dòng zhuāng xiāng |
| unboxing/ʌnˈbɒksɪŋ/ | 自动拆箱 | zì dòng chāi xiāng |
Boxing a primitive
- Some Java structures (like
ArrayList) hold objects, not primitives. - A wrapper class 包装类 wraps a primitive in an object:
Integerwrapsint,Doublewrapsdouble. Integer boxed = Integer.valueOf(5);puts theint5inside an object.- Wrappers let primitives live where only objects are allowed.
把基本类型装箱
- 有些 Java 结构(如
ArrayList)持有对象,而非基本类型。 - 包装类把基本类型包进一个对象:
Integer包int,Double包double。 Integer boxed = Integer.valueOf(5);把int5放进一个对象里。- 包装类让基本类型能存在于只允许对象的地方。
Autoboxing and unboxing
- Java converts automatically — autoboxing 自动装箱 and unboxing 自动拆箱.
Integer x = 5;autoboxes5;int y = x;unboxes it back.- So
list.add(5)works even though the list holdsIntegerobjects. - You rarely write
Integer.valueOfby hand — autoboxing does it.
自动装箱与自动拆箱
- Java 自动转换——自动装箱和自动拆箱。
Integer x = 5;自动装箱5;int y = x;把它拆回来。- 所以即使列表持有
Integer对象,list.add(5)也能工作。 - 你很少手写
Integer.valueOf——自动装箱替你做了。
Why ArrayList needs them
ArrayList<Integer>holdsIntegerobjects, because generics require a reference type.- You can't write
ArrayList<int>— primitives aren't objects. - Autoboxing lets you add and read plain
ints conveniently. Double,Boolean, etc. play the same role for their primitives.
为什么 ArrayList 需要它们
ArrayList<Integer>持有Integer对象,因为泛型需要引用类型。- 你不能写
ArrayList<int>——基本类型不是对象。 - 自动装箱让你方便地添加和读取普通
int。 Double、Boolean等为它们的基本类型扮演同样的角色。
Compare wrappers with equals
- Two
Integerobjects should be compared with.equals(), not==. ==compares references;.equals()compares the wrapped values.- (Java caches small Integers, so
==sometimes seems to work — don't rely on it.) - The same "use
.equals()for objects" rule fromStringapplies here.
用 equals 比较包装类
- 两个
Integer对象应该用.equals()比较,而非==。 ==比较引用;.equals()比较被包装的值。- (Java 缓存小的 Integer,所以
==有时看起来能工作——别依赖它。) - 来自
String的“对象用.equals()”规则在这里同样适用。
Wrappers are objects, so compare their values with .equals(), not ==. == tests reference identity; for two Integer objects with the same value it can be false. And you can't parameterize a collection with a primitive — it's ArrayList<Integer>, never ArrayList<int>. Autoboxing bridges the gap so you can still add plain ints.
包装类是对象,所以用 .equals()、而非 == 比较它们的值。== 检验引用同一性;对两个值相同的 Integer 对象它可能是 false。而且你不能用基本类型参数化一个集合——是 ArrayList<Integer>,绝不是 ArrayList<int>。自动装箱弥合了这个鸿沟,让你仍能添加普通 int。
Wrappers with an ArrayList:
ArrayList<Integer> nums = new ArrayList<Integer>();nums.add(5);— theint5is autoboxed into anInteger.int first = nums.get(0);— theIntegeris unboxed back toint.
ArrayList 里的包装类:
ArrayList<Integer> nums = new ArrayList<Integer>();nums.add(5);——int5被自动装箱成一个Integer。int first = nums.get(0);——Integer被自动拆箱回int。
A wrapper class (Integer, Double, Boolean) wraps a primitive in an object so it can go where objects are required — like an ArrayList<Integer> (you can't write ArrayList<int>). Autoboxing and unboxing convert automatically. Compare wrapper objects with .equals(), not ==.
包装类(Integer、Double、Boolean)把基本类型包进一个对象,使它能去需要对象的地方——如 ArrayList<Integer>(你不能写 ArrayList<int>)。自动装箱和自动拆箱自动转换。用 .equals()、而非 == 比较包装对象。
Autoboxing and unboxing · 自动装箱与自动拆箱
int ↔ Integer conversions happen automatically. · int ↔ Integer 转换自动发生。
Which wrapper class wraps an int? · 哪个包装类包装 int?
Integer wraps int; Double wraps double. · Integer 包 int;Double 包 double。
Why must an ArrayList of ints be declared ArrayList
You can't write ArrayList
Two Integer objects should be compared with... · 两个 Integer 对象应该用……比较。
Wrappers are objects — use .equals() for their values. · 包装类是对象——用 .equals() 比较它们的值。
In nums.add(5), the int 5 is automatically boxed into an Integer (autoboxing). · 在 nums.add(5) 中,int 5 被自动装箱成一个 Integer(自动装箱)。
Autoboxing converts the primitive to a wrapper object. · 自动装箱把基本类型转成一个包装对象。
A class that wraps a primitive in an object is a ___ class (one word). · 把基本类型包进对象的类是一个 ___ 类(填英文一词 wrapper)。
Integer, Double, Boolean are wrapper classes. · Integer、Double、Boolean 是包装类。