使用增强型 for 循环时数组是否会被转换?

发布于 2024-11-11 14:11:25 字数 132 浏览 5 评论 0原文

Java 5 或更高版本是否对数组应用某种形式的“装箱”?当下面的代码像 Iterable 一样遍历数组时,我想到了这个问题。

for( String : args ){
// Do stuff
}

Does Java 5 or higher apply some of form of "boxing" to arrays? This question came to mind as the following code goes through an array as if it's an Iterable.

for( String : args ){
// Do stuff
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人心善变 2024-11-18 14:11:25

数组不是基元。基元是不是对象的东西。只有 boolean、char、byte、short、int、long、float 和 double 是原语。数组是具有特殊语法的对象。如果你可以在 Java 中的类型上调用 toString() < 5.它并不原始。

所有自动装箱所做的就是将基元与需要对象(例如 ArrayList)的对象相互转换。它允许这样的代码:

List<Integer> l = new ArrayList<Integer>();
l.add( 7 );
int i = l.get(0);

简写为:

List<Integer> l = new ArrayList<Integer>();
Integer ii = Integer.valueOf( 7 )
l.add( ii );
int i = l.get(0).getIntValue();

这就是自动装箱所做的一切。由于 String 已经是一个对象(与 C 中不同的是,它不等于 char[]),因此无需对 String 进行装箱/拆箱。


编辑:如上所述添加布尔值

Arrays are not primitives. Primitives are things that are not objects. Only boolean, char, byte, short, int, long, float, and double are primitives. Arraysare objects with special syntax. If you can call toString() on type in Java < 5 it is not primitive.

All autoboxing does is convert primitives to/from objects that expect objects such as ArrayList. It allows code like:

List<Integer> l = new ArrayList<Integer>();
l.add( 7 );
int i = l.get(0);

To be short hand for:

List<Integer> l = new ArrayList<Integer>();
Integer ii = Integer.valueOf( 7 )
l.add( ii );
int i = l.get(0).getIntValue();

This is all autoboxing does. Since String is already an object (and is not equivalent to char[] unlike in C) there is nothing to box/unbox String to.


Edit: added boolean as mentioned

小鸟爱天空丶 2024-11-18 14:11:25

由于您似乎特别想知道增强的 for 循环,答案是它们是特殊情况的,而不是拆箱的。来自 §14.14.2

增强的 for 语句的形式为:

EnhancedForStatement:
    for (VariableModifiersopt 类型标识符: 表达式) 语句

表达式必须具有 Iterable 类型或者必须是数组类型(第 10.1 节),否则会出现编译时错误。

事实上,如果表达式是一个数组,它会被转换为等效的索引 for 循环,而不是实际使用迭代器。语言规范的同一部分涵盖了精确的扩展。

Since you seem to be wondering about enhanced for loops in particular, the answer is that they are special-cased, not unboxed. From §14.14.2:

The enhanced for statement has the form:

EnhancedForStatement:
    for ( VariableModifiersopt Type Identifier: Expression) Statement

The Expression must either have type Iterable or else it must be of an array type (§10.1), or a compile-time error occurs.

In fact, if the expression is an array, it is translated into the equivalent indexed for loop rather than actually using an iterator. The precise expansions are covered in the same section of the language spec.

Spring初心 2024-11-18 14:11:25

不,数组始终是引用类型。不需要装箱或拆箱,除非它是针对每个元素的访问。例如:

int[] x = new int[10]; // The value of x is a reference

int y = x[0]; // No boxing
Integer z = x[1]; // Boxing conversion from x[1] (which is an int) to Integer

另请注意,虽然增强的 for 循环可用于数组和可迭代对象,但它们的编译方式有所不同。

请参阅第 14.14.2 节 JLS 用于增强 for 循环的两种不同翻译。

No, arrays are always reference types. There's no need for boxing or unboxing, unless it's on the access for each element. For example:

int[] x = new int[10]; // The value of x is a reference

int y = x[0]; // No boxing
Integer z = x[1]; // Boxing conversion from x[1] (which is an int) to Integer

Also note that although the enhanced for loop is available for both arrays and iterables, it's compiled differently for each.

See section 14.14.2 of the JLS for the two different translations of the enhanced for loop.

伴随着你 2024-11-18 14:11:25

不。

与基元不同,数组(和字符串)是对象

No.

Unlike primitives, arrays (and strings) are Objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文