Indexing & length in Java
Zero-based indexes, .length field (not a method).
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; // writeYour turn
What prints?
String[] colors = {"red", "green", "blue"};
System.out.println(colors[2] + colors.length);green3blue3blue2
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)Your turn
What happens?
String s = "hey";
int[] a = {1, 2};
System.out.println(s.length() + a.length());Prints 5Prints 32Compile 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 errorExpressions 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]);20152510
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.
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 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
- First index is 0; last index is length - 1
- a.length is a field; s.length() is a String method
- Indexes must be int-compatible: byte, short, char or int, not long
- a[i] can be read or assigned
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);- Mon3
- Tue3
- Tue2
- 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());- 3
- 2
- Compile error
- 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().