1. Introduction
An array in Java is a container object that holds a fixed number of values of a single type. Once created, the size of an array cannot change, and every element must be the same declared type — this is why arrays are described as fixed-size and homogeneous.
Cricket analogy: A cricket array is like a printed 11-player scorecard slate - fixed at 11 slots the moment it's handed to the umpire, and every slot must hold a batsman's name, never a coach's.
Even though arrays hold primitive values or object references, in Java arrays are themselves objects. They are created on the heap using the 'new' keyword (or an array literal), they have a public final field called length, and their default value is null when declared as a reference without initialization.
Cricket analogy: A Team[] array is itself an object sitting in the heap like a franchise's official roster binder, created with new or listed directly, carrying a public final length field showing squad size, and defaulting to null if the binder was never issued.
2. Syntax
// Declaration and allocation
int[] arr = new int[5]; // preferred style
int arr2[] = new int[5]; // C-style, also legal
// Declaration with initialization (array literal)
int[] nums = {10, 20, 30, 40, 50};
// Declaring then assigning
int[] scores;
scores = new int[]{1, 2, 3};3. Explanation
When you write 'new int[5]', Java allocates space for 5 integers on the heap and initializes every slot to that type's default value: 0 for numeric types, 0.0 for float/double, false for boolean, '\u0000' for char, and null for any reference/object type array. Arrays are zero-indexed, so valid indices for a length-5 array run from 0 to 4.
Cricket analogy: new int[5] is like reserving 5 blank scorecard boxes before the toss, each auto-filled with a default 0 runs, boolean fields defaulting to false ("not out"), and numbered box 0 through box 4 for the top five batsmen.
Accessing an index outside the valid range (negative, or >= length) does not silently fail — it throws an ArrayIndexOutOfBoundsException at runtime, since Java performs bounds checking on every array access.
Cricket analogy: Trying to read batsman index 11 on an 11-player scorecard (indices 0-10) throws an ArrayIndexOutOfBoundsException at runtime, just as the umpire would reject a 12th name on a locked lineup - Java checks every access.
Classic exam trap: arr.length is a FIELD (no parentheses), while String's length() is a METHOD. Writing arr.length() is a compile-time error, and writing str.length is also a compile-time error. Mixing these up is one of the most common beginner mistakes.
Because arrays are objects, an array variable actually holds a reference to the array on the heap. Assigning one array variable to another (b = a) copies the reference, not the elements — both variables then point to the same underlying array.
4. Example
public class ArrayDemo {
public static void main(String[] args) {
int[] scores = new int[4]; // default values: 0,0,0,0
System.out.println("Default: " + scores[0]);
int[] marks = {85, 90, 78, 92};
System.out.println("Length: " + marks.length);
for (int i = 0; i < marks.length; i++) {
System.out.println("marks[" + i + "] = " + marks[i]);
}
try {
System.out.println(marks[10]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}5. Output
Default: 0
Length: 4
marks[0] = 85
marks[1] = 90
marks[2] = 78
marks[3] = 92
Caught: Index 10 out of bounds for length 46. Key Takeaways
- Arrays in Java have a fixed size determined at creation time and cannot be resized.
- Arrays are objects stored on the heap and expose a public final field 'length' (not a method).
- Numeric elements default to 0, boolean to false, and object references to null on creation.
- Array indices are zero-based; out-of-range access throws ArrayIndexOutOfBoundsException.
- Array variables hold references — copying the variable does not copy the underlying data.
- Use 'new type[size]' or array-literal syntax '{v1, v2, ...}' to create arrays.
Practice what you learned
1. What is the default value of an uninitialized element in an int[] array?
2. Which of the following correctly retrieves the number of elements in an array named 'data'?
3. What happens when you access marks[10] on an array declared as int[] marks = new int[5]?
4. In Java, arrays are best described as:
5. What does the following code print?\nint[] a = {1,2,3};\nint[] b = a;\nb[0] = 99;\nSystem.out.println(a[0]);
Was this page helpful?
You May Also Like
Multidimensional Arrays in Java
Understand how Java implements multidimensional arrays as arrays of arrays, enabling jagged arrays and matrix-style data access.
Strings in Java
Understand Java's String class, its immutability, the string constant pool, and why == should never be used to compare string content.
ArrayList in Java
Learn how ArrayList works internally, its time complexity, and when to use it over arrays or LinkedList.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics