HugeArray.java


Below is the syntax highlighted version of HugeArray.java from §1.4 Arrays.


/******************************************************************************
 *  Compilation:  javac HugeArray.java
 *  Execution:    java HugeArray
 *
 *  Attempts to create an array of size n^4 for n = 1000.
 *
 *  This program compiles cleans.
 *  When n is 1000, it leads to the following error
 *
 *      java.lang.NegativeArraySizeException
 *
 *  because 1000^4 overflows an int and results in a negative integer.
 *
 *
 *
 *  When n is 200, it leads to the following error
 *
 *      java.lang.OutOfMemoryError: Requested array size exceeds VM limit
 *
 *
 ******************************************************************************/

public class HugeArray {

    public static void main(String[] args) {
        int n = 1000;
        int[] a = new int[n*n*n*n];
        System.out.println(a.length);
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:13:44 EDT 2022.