Arrays in memory: int[] versus Integer[]
35 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A Java array is an object like any other: a header, then its elements one after another. What changes the memory is what an element is. In an int[] each element is the number itself; in an Integer[] each element is only a reference to a separate Integer object somewhere else on the heap.
The array header and its length field
An array header is the normal object header plus a 4-byte length field. On 64-bit HotSpot (Java 17 and 21, default settings) that is 12 + 4 = 16 bytes, and the elements start right after it.
Example 1 · Arrays are objects with a length in the header
public class ArrayHeaderDemo { // 64-bit HotSpot, Java 17 and 21, defaults: 12-byte object header + 4-byte length = 16 static final int ARRAY_HEADER = 16;
static int arraySize(int length, int elementBytes) { int raw = ARRAY_HEADER + length * elementBytes; return (raw + 7) / 8 * 8; }
public static void main(String[] args) { int[] empty = new int[0]; int[] marks = new int[10]; System.out.println("empty.length = " + empty.length + ", marks.length = " + marks.length); System.out.println("Class names: " + empty.getClass().getName() + " and " + new Integer[0].getClass().getName());
System.out.println("new int[0]: " + arraySize(0, 4) + " bytes"); System.out.println("new int[1]: " + arraySize(1, 4) + " bytes"); System.out.println("new int[3]: " + arraySize(3, 4) + " bytes"); System.out.println("new int[10]: " + arraySize(10, 4) + " bytes"); System.out.println("new byte[5]: " + arraySize(5, 1) + " bytes"); }}empty.length = 0, marks.length = 10 Class names: [I and [Ljava.lang.Integer; new int[0]: 16 bytes new int[1]: 24 bytes new int[3]: 32 bytes new int[10]: 56 bytes new byte[5]: 24 bytes
What this code does
lengthis read from the 16-byte header, sonew int[0]is still a real 16-byte object. Arrays have class names too:[Imeans "array of int",[Ljava.lang.Integer;means "array of Integer".new int[1]is 16 + 4 = 20 bytes, rounded up to 24.new byte[5]is 16 + 5 = 21, also 24.
This is where arr.length comes from: it is read from the header, not counted. The length is fixed when the array is created, which is why an array can never grow. An array's size is roundUpTo8(16 + length × element size), so even new int[0] takes 16 bytes.
int[] stores values side by side
A primitive array such as int[], byte[] or double[] stores the values themselves in one block, one after another.
Example 2 · Primitive arrays: one object, values side by side
public class PrimitiveArraySizesDemo { static long arraySize(long length, int elementBytes) { long raw = 16 + length * elementBytes; // 16-byte array header return (raw + 7) / 8 * 8; }
public static void main(String[] args) { int students = 1000; System.out.println("Storing " + students + " values in one array:"); System.out.println("byte[] " + arraySize(students, 1) + " bytes (attendance: 0 or 1)"); System.out.println("char[] " + arraySize(students, 2) + " bytes (grade letters)"); System.out.println("int[] " + arraySize(students, 4) + " bytes (marks)"); System.out.println("double[] " + arraySize(students, 8) + " bytes (CGPA)");
int[] marks = new int[students]; for (int i = 0; i < students; i++) { marks[i] = 40 + i % 60; // the values sit side by side in one block } System.out.println("marks[0] = " + marks[0] + ", marks[999] = " + marks[999]); }}Storing 1000 values in one array: byte[] 1016 bytes (attendance: 0 or 1) char[] 2016 bytes (grade letters) int[] 4016 bytes (marks) double[] 8016 bytes (CGPA) marks[0] = 40, marks[999] = 79
What this code does
- The same 1000 values cost 1016, 2016, 4016 or 8016 bytes depending on the element type. Pick the smallest type that safely holds your values.
marksis a single object. Filling it creates no new objects, because eachintis written straight into its slot.
new int[1000] is one object of 16 + 4000 = 4016 bytes, and marks[i] is found by simple arithmetic: start of the elements + i × 4. Nothing else is allocated. There are no extra objects for the GC to track, and reading the values in order is fast because they sit next to each other in memory. Each element starts as 0 (or false, or 0.0).
- Element sizes:
byte/boolean1,short/char2,int/float4,long/double8 bytes. - Choosing a smaller type shrinks the array: 1000 attendance flags take 1016 bytes as
byte[]orboolean[], 4016 asint[].
Integer[] stores references to separate objects
An array of objects such as Integer[] or String[] stores references, not values. Each slot is a 4-byte compressed reference that points to an object elsewhere on the heap, or holds null.
Example 3 · Integer[] slots are references: null, shared, unboxed
import java.util.Arrays;
public class BoxedArrayDemo { public static void main(String[] args) { int[] plain = new int[3]; Integer[] boxed = new Integer[3]; System.out.println("int[] starts as " + Arrays.toString(plain)); System.out.println("Integer[] starts as " + Arrays.toString(boxed));
Integer hostelFee = 45000; // one Integer object on the heap boxed[0] = hostelFee; boxed[1] = hostelFee; // the same object, stored twice System.out.println("Slots 0 and 1 point to one object? " + (boxed[0] == boxed[1]));
int total = 0; try { for (Integer fee : boxed) { total += fee; // unboxing calls fee.intValue() } } catch (NullPointerException e) { System.out.println("NullPointerException: slot 2 holds no Integer"); } System.out.println("Total before the crash: " + total); }}int[] starts as [0, 0, 0] Integer[] starts as [null, null, null] Slots 0 and 1 point to one object? true NullPointerException: slot 2 holds no Integer Total before the crash: 90000
What this code does
- A new
int[]holds zeros, but a newInteger[]holdsnull: no objects exist yet. Slots 0 and 1 hold the same reference, so==istrue. total += feeunboxes withfee.intValue(). For the empty slot 2 that call fails withNullPointerException, after adding 45000 twice.
Example 4 · Measured: int[] versus Integer[] for a million values
public class IntVsIntegerArrayDemo { static long usedAfterGc() { System.gc(); // a request, not a promise Runtime rt = Runtime.getRuntime(); return rt.totalMemory() - rt.freeMemory(); }
public static void main(String[] args) { int n = 1_000_000;
long before = usedAfterGc(); int[] plain = new int[n]; for (int i = 0; i < n; i++) { plain[i] = 1000 + i; } long plainBytes = usedAfterGc() - before;
before = usedAfterGc(); Integer[] boxed = new Integer[n]; for (int i = 0; i < n; i++) { boxed[i] = 1000 + i; // a new Integer for each value } long boxedBytes = usedAfterGc() - before;
System.out.println("int[] about " + plainBytes / 1024 + " KB"); System.out.println("Integer[] about " + boxedBytes / 1024 + " KB"); System.out.printf("Integer[] is about %.1f times bigger%n", (double) boxedBytes / plainBytes); System.out.println(plain[n - 1] + " " + boxed[n - 1]); // keeps both arrays in use }}int[] about 3625 KB Integer[] about 19732 KB Integer[] is about 5.4 times bigger 1000999 1000999
What this code does
- The estimate for
int[]is 16 + 4,000,000 bytes, about 3906 KB. ForInteger[]add 1,000,000 × 16 bytes ofIntegerobjects: about 19,531 KB. The measurement is rough (see the previous lesson), so this run shows a little less forint[]. - Every value is 1000 or more, so each one is a new
Integerobject. Your numbers will differ slightly from run to run.
For numbers that costs a lot. Integer[] with a million different values is the array (16 + 4 × 1,000,000 bytes) plus a million 16-byte Integer objects: about 19 MB instead of about 4 MB for int[], roughly 5 times more. It also gives the GC a million extra objects to track, and every read follows a pointer to a different place in memory. Slots start as null, and unboxing a null slot throws NullPointerException. Two slots can also point to the same object.
2D arrays are arrays of arrays
A 2D array in Java is an array whose elements are references to other arrays. new int[3][4] creates four objects: one outer int[][] with 3 references, and three separate int[4] rows.
Example 5 · A 2D array is an outer array of row arrays
public class TwoDArrayDemo { static int arraySize(int length, int elementBytes) { return (16 + length * elementBytes + 7) / 8 * 8; }
public static void main(String[] args) { int[][] marks = new int[3][4]; // 3 students, 4 subjects System.out.println("marks.length = " + marks.length + ", marks[0].length = " + marks[0].length); System.out.println("Rows are separate objects? " + (marks[0] != marks[1]));
int outer = arraySize(3, 4); int rows = 3 * arraySize(4, 4); System.out.println("int[3][4]: " + outer + " + " + rows + " = " + (outer + rows) + " bytes, 4 objects"); System.out.println("int[12]: " + arraySize(12, 4) + " bytes, 1 object");
int[][] fest = new int[3][]; // jagged: rows of different lengths fest[0] = new int[5]; fest[1] = new int[2]; System.out.println("Row lengths: " + fest[0].length + ", " + fest[1].length + ", row 2 is " + fest[2]);
int[] shared = new int[2]; int[][] alias = {shared, shared}; // two rows, one array alias[0][0] = 99; System.out.println("alias[1][0] = " + alias[1][0]); }}marks.length = 3, marks[0].length = 4 Rows are separate objects? true int[3][4]: 32 + 96 = 128 bytes, 4 objects int[12]: 64 bytes, 1 object Row lengths: 5, 2, row 2 is null alias[1][0] = 99
What this code does
marks[0]andmarks[1]are differentint[]objects. The 3 × 4 grid costs 128 bytes in 4 objects; a flatint[12]would cost 64 bytes in one object.new int[3][]creates only the outer array, sofest[2]is stillnull. Inalias, both rows are the same array, so writingalias[0][0]also changesalias[1][0].
So int[3][4] takes 32 + 3 × 32 = 128 bytes, while a flat int[12] holding the same 12 numbers takes 64. Each row has its own header, rows can have different lengths (a jagged array), a row can be null, and two rows can even be the same array object. The shape matters: int[1000][2] is 1001 objects with 1000 row headers, while int[2][1000] is just 3 objects. When rows are short, put the long dimension last or use one flat array with row * cols + col.
Trace the code
Follow a two-dimensional array
Variables
- marks
- [[0, 0, 0], [0, 0, 0]]
Output
(nothing printed yet)Line 3new int[2][3] creates three objects: an outer array of two references and two inner int arrays.
Next step
Check your understanding
12 quick questions