Well, Ivan has already correctly pointed out that the array length does have a well defined upper limit and that it is again JVM/Platform dependent. In fact, more importantly, he also stated that how much length of array you can actually create in your code will mainly be controlled by how much max heap space you have allocated to your program while execution.
I would just like to add small code snippet to support his explanation. For example, theoretically, an array [] should accept length <= INTEGER.MAX_VALUE - x (here x is header size that is again JVM/Platform specific) but suppose you run following Java program with VM option -Xmx32m then you will see that none of the created array reach the length close to MAX_ARRAY_SIZE (i.e. 2147483639)
byte[] array : 1 byte
0 l=1048576 s=1mb
1 l=2097152 s=2mb
2 l=4194304 s=4mb
3 l=8388608 s=8mb
java.lang.OutOfMemoryError: Java heap space l=16777216 s=16mb
char[] array : 2 byte
0 l=1048576 s=2mb
1 l=2097152 s=4mb
2 l=4194304 s=8mb
java.lang.OutOfMemoryError: Java heap space l=8388608 s=16mb
int[] array : 4 byte
0 l=1048576 s=4mb
1 l=2097152 s=8mb
java.lang.OutOfMemoryError: Java heap space l=4194304 s=16mb
double[] array : 8 byte
0 l=1048576 s=8mb
java.lang.OutOfMemoryError: Java heap space l=2097152 s=16mb
Below is the code:
byte[] barray = null;
System.out.println("\nbyte[] array : 1 byte");
try {
for (ii=0; ii < 32; ii++) {
barray = new byte[(int)Math.pow(2, ii)*1024*1024];
System.out.println(ii + " l=" + barray.length + " s=" + barray.length / (1024 * 1024) + "mb");
}
}
catch (Throwable e) {
barray = null;
System.out.println(e + " l=" + (int)Math.pow(2, ii)*1024*1024 + " s=" + (int)Math.pow(2, ii)*1024*1024 / (1024 * 1024) + "mb");
}
char[] carray = null;
System.out.println("\nchar[] array : 2 byte");
try {
for (ii=0; ii < 32; ii++) {
carray = new char[(int)Math.pow(2, ii)*1024*1024];
System.out.println(ii + " l=" + carray.length + " s=" + 2*carray.length / (1024 * 1024) + "mb");
}
}
catch (Throwable e) {
carray = null;
System.out.println(e + " l=" + (int)Math.pow(2, ii)*1024*1024 + " s=" + 2*(int)Math.pow(2, ii)*1024*1024 / (1024 * 1024) + "mb");
}
int[] iarray = null;
System.out.println("\nint[] array : 4 byte");
try {
for (ii=0; ii < 32; ii++) {
iarray = new int[(int)Math.pow(2, ii)*1024*1024];
System.out.println(ii + " l=" + iarray.length + " s=" + 4*iarray.length / (1024 * 1024) + "mb");
}
}
catch (Throwable e) {
iarray = null;
System.out.println(e + " l=" + (int)Math.pow(2, ii)*1024*1024 + " s=" + 4*(int)Math.pow(2, ii)*1024*1024 / (1024 * 1024) + "mb");
}
double[] darray = null;
System.out.println("\ndouble[] array : 8 byte");
try {
for (ii=0; ii < 32; ii++) {
darray = new double[(int)Math.pow(2, ii)*1024*1024];
System.out.println(ii + " l=" + darray.length + " s=" + 8*darray.length / (1024 * 1024) + "mb");
}
}
catch (Throwable e) {
darray = null;
System.out.println(e + " l=" + (int)Math.pow(2, ii)*1024*1024 + " s=" + 8*(int)Math.pow(2, ii)*1024*1024 / (1024 * 1024) + "mb");
}