Everything you have written so far uses one variable per value. That works until you need to hold thirty marks, and then it collapses: thirty declarations, thirty input lines, thirty additions, and a rewrite the moment a thirty-first student appears. An array fixes this. It is a single block of memory that holds many values of the same type, reached by position rather than by name, so the same three lines of code work for three values or three thousand.
// Without an array - unusable past a handful of values
int mark1 = 88;
int mark2 = 92;
int mark3 = 75;
int total = mark1 + mark2 + mark3;
// With an array - the same idea, but it scales
int[] marks = {88, 92, 75};
int total2 = marks[0] + marks[1] + marks[2];There are two ways to make an array, and you choose between them based on whether you already know the values. The square brackets in int[] are part of the type: int[] is read as "array of int", a different type from plain int, and everything inside must be that one type. Note that the short initializer form {1, 2, 3} is only legal on the declaration line — splitting it into int[] a; then a = {1, 2, 3}; is a compile error, and you would have to write a = new int[]{1, 2, 3}; instead.
// Form 1: you know the SIZE but not the values yet.
// Java allocates 10 slots and fills them with the default value 0.
int[] scores = new int[10];
// Form 2: you know the VALUES. Java counts them and sizes the array for you.
int[] scores2 = {88, 92, 75}; // length 3
// Other types work exactly the same way
double[] temps = new double[7];
String[] names = {"Aisha", "Ben", "Chloe"};
boolean[] present = new boolean[30];The position of an element is its index, and indexes begin at 0, not 1. So an array of length 5 has indexes 0, 1, 2, 3 and 4 — there is no index 5. This feels arbitrary until you know where it comes from: the index is really an offset from the start of the memory block. The first element sits zero slots along from the start, the second sits one slot along, and so on. Once you think of it as an offset, zero-based indexing is the only version that makes sense.
public class ScoreDemo {
public static void main(String[] args) {
int[] scores = new int[5];
scores[0] = 88;
scores[1] = 92;
scores[2] = 75;
scores[3] = 61;
scores[4] = 94;
System.out.println("First: " + scores[0]);
System.out.println("Third: " + scores[2]);
System.out.println("Last: " + scores[scores.length - 1]);
System.out.println("How many: " + scores.length);
}
}arr[arr.length - 1] whenever you want the last element. This is not a trick, it is the safest habit in array code: it stays correct when the array changes size, and it is the phrase that stops you from ever writing arr[arr.length].An array reports its size through .length with no parentheses, because length is a stored property of the array object. A String reports its size through .length() with parentheses, because that is a method call. Separately, new int[10] does not leave the slots full of garbage: Java guarantees every element starts at the zero value for its type, which is why you can safely add up a fresh int[] and get 0 rather than nonsense. The one that catches people is String[], whose default is null rather than the empty string — printing an unassigned element shows the word null, and calling a method on it throws a NullPointerException.
| What you have | How you get its size | Why |
|---|---|---|
int[] arr | arr.length | A field stored on the array — no brackets |
String s | s.length() | A method the String class provides — brackets required |
arr.length() | Compile error | An array has no method called length |
s.length | Compile error | A String has no public field called length |
| Element type | Default value | What prints |
|---|---|---|
int, short, long | 0 | 0 |
double, float | 0.0 | 0.0 |
boolean | false | false |
char | the null character \u0000 | an invisible blank |
String and any object | null | null |
Once created, an array's length can never change. new int[10] reserves exactly ten slots for the life of that array; there is no .add() and no way to grow it. If you need an eleventh value you must create a larger array and copy everything across. That sounds like a flaw, and in exchange you get something valuable: because the size and the type are fixed, the computer knows the exact address of element i by arithmetic alone, so reading arr[5000] costs the same as reading arr[0].
- Use when the values are not known yet
- You must supply the size as a number or a variable
- Every slot is filled with the type's default
int[] daily = new int[days];where days was read from the user
- Use when the values are known as you write the code
- Java counts the values and sets the length for you
- Only legal on the declaration line
int[] sides = {3, 4, 5};
int[] arr = {10, 20, 30}; // length 3, valid indexes 0, 1, 2
System.out.println(arr[2]); // 30 - fine, this is the last element
System.out.println(arr[3]); // CRASH: Index 3 out of bounds for length 3arr[arr.length] is not a value that is sometimes wrong; it is guaranteed to throw ArrayIndexOutOfBoundsException. The correct expression for the last element is arr[arr.length - 1].