Are arrays being transformed when using an enhanced for loop?
Asked Answered
D

4

1

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
}
Doubly answered 2/6, 2011 at 15:36 Comment(2)
I'd recommend you take a look at this download.oracle.com/javase/1.5.0/docs/guide/language/…Precess
There is no boxing or unboxing in your example. This is an enhanced for-each loop.Laura
E
2

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.

Export answered 2/6, 2011 at 15:38 Comment(1)
Thanks Jon. Having a stronger background in C would help to better understand the subtleties of each language. I wrongly assumed a one-size-fits-all approach was being used and something implicit was taking place.Doubly
B
5

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

Bisitun answered 2/6, 2011 at 15:38 Comment(6)
"Primitives are things that are not objects" Expressed like that it's true that arrays can't be qualified as primitives in any way. Thank you for your examples.Doubly
"Expressed like that it's true that arrays can't be qualified as primitives in any way." Moreover: Arrays inherit from Object.Sabinesabino
@Sabinesabino Thanks, I forgot.Bisitun
@trutheality: They appear to but now I think of it there doesn't seem to be a class that represents an array. Are they considered as direct instances of Object (as in not being a child of)?Doubly
@James P.: They have funny names like [I for int[] and [Ljava.lang.String; with the semicolon for String[].Sabinesabino
@JamesP There is not class for Arrays but they have their own type. Try this "System.out.println( new int[0].getClass() );" You will get "[I". This means that its type is "Array of Ints". "int[0][0]" returns "[[I". In general arrays are really weird objects with their own rules so often that people often speak of them as primitives.Bisitun
N
3

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.

Norrie answered 2/6, 2011 at 15:40 Comment(2)
Ah ok. So the for loop actually handles each type separately.Doubly
Yes. I think it would be useful if arrays (maybe only reference arrays) would also implement the Iterable interface, so we can use them more generally instead of having to duplicate all methods which want to work on both array and Iterable.Papain
E
2

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.

Export answered 2/6, 2011 at 15:38 Comment(1)
Thanks Jon. Having a stronger background in C would help to better understand the subtleties of each language. I wrongly assumed a one-size-fits-all approach was being used and something implicit was taking place.Doubly
C
1

No.

Unlike primitives, arrays (and strings) are Objects.

Cargill answered 2/6, 2011 at 15:37 Comment(1)
Indeed. This brings the question of how an array is actually represented as, contrary to String, it doesn't seem to have it's own specialized class in the Javadoc. java.sun.com/docs/books/jls/second_edition/html/arrays.doc.htmlDoubly

© 2022 - 2024 — McMap. All rights reserved.