13

I want to find out if length property for Java arrays is an int/long or something else.

5 Answers 5

20

It is an int. See the Java Language Specification, section 10.7.

1
  • I really can't see anything forbidding the use of a long, for instance on a platform where that is more performant. You just can't index it with anything bigger than an int. It's a theoretical discussion at this point though. (Yeah, I saw the example but that's not normative).
    – extraneon
    Commented Dec 15, 2010 at 19:10
6

In Java Language spec, Arrays you can see in 10.4:

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion and become int values. An attempt to access an array component with a long index value results in a compile-time error.

I could not find the type of the length attribute, but it is at least an int; and if it's a long then you can not access elements beyond the max integer length.

So I guess it's a (final) int.

2

According to the specification, it's an int

1

The data type is int, not long. Same as the index.

See http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html, 10.4

0

In JavaCard array indexes are shorts, but JavaCard is odd like that. Everywhere else, int like everyone else says.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.