🧱 Arrays · Beginner

Indexing & length in Java

Zero-based indexes, .length field (not a method).

🧩 The mysteryAn array of 3 days: days[3] crashes, days[1] isn't the first day, and days.length() won't compile. Three tiny rules. Let's crack them.

Counting from zero

Array elements are numbered from 0. The first is a[0], the second a[1], and the last is **a[a.length - 1]. Think of the index as "how many steps from the start"**. You read and write elements with the same a[i] syntax.

int[] a = {10, 20, 30};
int first = a[0];            // 10
int last = a[a.length - 1];  // 30
a[1] = 25;                   // write
🔮 Predict it

Your turn

What prints?

String[] colors = {"red", "green", "blue"};
System.out.println(colors[2] + colors.length);
  1. green3
  2. blue3
  3. blue2
Show the answer

Index 2 is the third element, "blue", and the array has 3 elements. String + int glues them together: blue3.

length: a field, not a method

For arrays, length is a field: write a.length with no parentheses. Strings are the opposite: s.length() is a method. Mixing them up is a compile error.

int[] a = {1, 2, 3};
String s = "hey";
a.length    // 3   (field)
s.length()  // 3   (method)
🔮 Predict it

Your turn

What happens?

String s = "hey";
int[] a = {1, 2};
System.out.println(s.length() + a.length());
  1. Prints 5
  2. Prints 32
  3. Compile error
Show the answer

Compile error. a.length() treats an array's length as a method, but it's a field. s.length() is fine. Fix: s.length() + a.length.

Indexes are ints

An index can be any int expression: a[i + 1], a[a.length - 1], a[5 / 2]. It must convert to int without narrowing: byte, short and char work (a['A'] is a[65]), but a **long doesn't compile**.

int[] a = new int[100];
a['A'] = 1;      // a[65]
a[(byte) 2] = 1; // fine
a[5 / 2] = 1;    // a[2]
a[3L] = 1;       // compile error
🔮 Predict it

Expressions as indexes

What prints?

int[] a = {5, 10, 15, 20};
int i = 2;
a[i - 1] = a[i] + a[0];
System.out.println(a[1]);
  1. 20
  2. 15
  3. 25
  4. 10
Show the answer

a[i] is a[2] = 15 and a[0] = 5, so 20 is stored into a[i - 1], which is a[1]. Work out the index first, then the slot.

⚠️ The trap

One past the end

a[a.length] is not the last element: it's one step *past* the end. With 3 elements the valid indexes are 0, 1 and 2, so a[3] doesn't exist. The last element is always a[a.length - 1].

int[] a = {10, 20, 30};
a[a.length - 1]; // 30, the last one
a[a.length];     // a[3]: doesn't exist!
💼 In the real world

In real projects

Zero-based indexing is everywhere: Java, C, Python, JavaScript, string positions, list indexes. Mixing it with human counting ("item 1") is a constant source of off-by-one bugs, like showing "page 0" to users or skipping the first row of a report.

Key takeaways

  1. First index is 0; last index is length - 1
  2. a.length is a field; s.length() is a String method
  3. Indexes must be int-compatible: byte, short, char or int, not long
  4. a[i] can be read or assigned
🤯 Did you know?

In 1982 Edsger Dijkstra wrote a famous note, "Why numbering should start at zero", arguing that 0-based indexes make ranges cleaner and less error-prone.

Practice questions

What does this print?

String[] days = {"Mon", "Tue", "Wed"};
System.out.println(days[1] + days.length);
  1. Mon3
  2. Tue3
  3. Tue2
  4. Wed3
Check your answer

Tue3. Index 1 is the second element, "Tue", and the array has 3 elements.

What does this print?

int[] a = {1, 2, 3};
System.out.println(a.length());
  1. 3
  2. 2
  3. Compile error
  4. Throws NoSuchMethodError
Check your answer

Compile error. length is a field on arrays, not a method, so a.length() doesn't compile. Strings are the opposite: s.length().

Next: what happens when you step one slot past the end, and how Java's answer blocks a famous class of security bugs.