Skip to content
UniCircle
EasyConceptualArrays

Arrays in Java: declaration, defaults and pitfalls

Question JAVA-ARR-023

How are arrays created in Java, what are their default values, and what is the difference between length, length() and size()?

Show the explanation

Arrays are objects with a fixed length set at creation. They are stored on the heap and their elements get default values: 0 for numbers, false for boolean, '\u0000' for char and null for references.

Java
int[] marks = new int[3];                 // {0, 0, 0}
String[] names = {"Asha", "Ravi"};        // initialiser
int[][] grid = new int[2][3];             // 2 rows of 3
int[][] jagged = { {1}, {2, 3}, {4, 5, 6} };   // rows of different lengths
  • array.length — a field of arrays (no brackets).
  • string.length() — a method of String.
  • list.size() — a method of collections.

Pitfalls: reading index length throws ArrayIndexOutOfBoundsException; printing an array directly gives something like [I@1b6d3586 (use Arrays.toString or Arrays.deepToString); == and .equals() on arrays compare identity (use Arrays.equals); and Arrays.asList(intArray) gives a list with one element (the array), because primitives are not boxed.

Original UniCircle question. Last reviewed 22 Oct 2026.