Considering the String
class’ length
method returns an int
, the maximum length that would be returned by the method would be Integer.MAX_VALUE
, which is 2^31 - 1
(or approximately 2 billion.)
In terms of lengths and indexing of arrays, (such as char[]
, which is probably the way the internal data representation is implemented for String
s), Chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition says the following:
The variables contained in an array
have no names; instead they are
referenced by array access expressions
that use nonnegative integer index
values. These variables are called the
components of the array. If an array
hasn
components, we sayn
is the
length of the array; the components of
the array are referenced using integer
indices from0
ton - 1
, inclusive.
Furthermore, the indexing must be by int
values, as mentioned in Section 10.4:
Arrays must be indexed by
int
values;
Therefore, it appears that the limit is indeed 2^31 - 1
, as that is the maximum value for a nonnegative int
value.
However, there probably are going to be other limitations, such as the maximum allocatable size for an array.