Your array has 8 elements, so array.length=8.

Therefore array.length-1 = 8-1 = 7.

You are running the loop from when i is 0, until i is less than 7. So it will run from i=0 to i=6.

To traverse the entire array, you need to run it from i=0 to i=7 instead.

So change

i< array.length -1

to

i < array.length 

or

i <= array.length - 1
Answer from Kartik on Stack Overflow
Top answer
1 of 8
131

Let me first highlight three different ways for similar purpose.

length -- arrays (int[], double[], String[]) -- to know the length of the arrays

length() -- String related Object (String, StringBuilder, etc) -- to know the length of the String

size() -- Collection Object (ArrayList, Set, etc) -- to know the size of the Collection

Now forget about length() consider just length and size().

length is not a method, so it completely makes sense that it will not work on objects. It only works on arrays.
size() its name describes it better and as it is a method, it will be used in the case of those objects who work with collection (collection frameworks) as I said up there.

Now come to length():
String is not a primitive array (so we can't use .length) and also not a Collection (so we cant use .size()) that's why we also need a different one which is length() (keep the differences and serve the purpose).

As answer to Why?
I find it useful, easy to remember and use and friendly.

2 of 8
27

A bit simplified you can think of it as arrays being a special case and not ordinary classes (a bit like primitives, but not). String and all the collections are classes, hence the methods to get size, length or similar things.

I guess the reason at the time of the design was performance. If they created it today they had probably come up with something like array-backed collection classes instead.

If anyone is interested, here is a small snippet of code to illustrate the difference between the two in generated code, first the source:

public class LengthTest {
  public static void main(String[] args) {
    int[] array = {12,1,4};
    String string = "Hoo";
    System.out.println(array.length);
    System.out.println(string.length());
  }
}

Cutting a way the not so important part of the byte code, running javap -c on the class results in the following for the two last lines:

20: getstatic   #3; //Field java/lang/System.out:Ljava/io/PrintStream;
23: aload_1
24: arraylength
25: invokevirtual   #4; //Method java/io/PrintStream.println:(I)V
28: getstatic   #3; //Field java/lang/System.out:Ljava/io/PrintStream;
31: aload_2
32: invokevirtual   #5; //Method java/lang/String.length:()I
35: invokevirtual   #4; //Method java/io/PrintStream.println:(I)V

In the first case (20-25) the code just asks the JVM for the size of the array (in JNI this would have been a call to GetArrayLength()) whereas in the String case (28-35) it needs to do a method call to get the length.

In the mid 1990s, without good JITs and stuff, it would have killed performance totally to only have the java.util.Vector (or something similar) and not a language construct which didn't really behave like a class but was fast. They could of course have masked the property as a method call and handled it in the compiler but I think it would have been even more confusing to have a method on something that isn't a real class.

🌐
Quora
quora.com › When-should-I-use-array-length-vs-array-length-1-in-a-for-loop
When should I use array.length vs array.length-1 in a for loop? - Quora
B.S in Computer Science & Electronics ... · 5y · Use array.length for calculations of size, and array.length-1 for access to elements within the array....
🌐
Edureka
edureka.co › blog › array-length-in-java
Array Length In Java | Java Array Examples | Edureka
December 4, 2023 - The length of the array is: 1 · It must be noted that on accessing the length field of an empty or a null object, a NullPointerException is raised. The array length has many useful properties, that can be used while programming. In the following example, we use the length of the array to loop through all the elements and to determine whether the specific value is present. /** * An Example that uses Java Array Length to check if the array contains a * specific value.
🌐
Alvin Alexander
alvinalexander.com › java › java-array-length-example-length-method
A Java array length example | alvinalexander.com
While you might logically expect there to be a length method on a Java array, there is actually a public length attribute on an array (instead of a length method).
🌐
TutorialsPoint
tutorialspoint.com › What-is-length-in-Java-Arrays
What is length in Java Arrays?
Length is a filed in java, it gives the total number of the elements in a Java array. The length of an array is defined after the array was created. Integer[] myArray = {23, 93, 56, 92, 39}; System.out.println(myArray.length); Monica Mona · Updated on: 2020-02-19T10:41:06+05:30 ·
🌐
HWS Math
math.hws.edu › javanotes › c7 › s1.html
Javanotes 9, Section 7.1 -- Array Details
The elements of the array A are A[0], A[1], ..., A[A.length-1]. An attempt to refer to an array element with an index outside the range from zero to A.length-1 causes an ArrayIndexOutOfBoundsException. Arrays in Java are objects, so an array variable can only refer to an array; it does not ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java tutorial for beginners: 100+ hands-on java video tutorials › java array length tutorial with code examples
Java Array Length Tutorial With Code Examples
April 1, 2025 - The loop for this will iterate through all the elements one by one till (length-1) the element is reached (since arrays start from 0). Using this loop you can search if a specific value is present in the array or not.
Find elsewhere
🌐
LinkedIn
linkedin.com › pulse › how-java-stores-array-length-safely-om-kudalkar
How java stores array length safely.
March 10, 2021 - In java, to find the length of an array, we use arr.length and not arr.length(). If you notice, this is a field access. Since, length does not have parentheses after it, this tells me that it’s not a method, so it has to be a field. Well, how does Java keep track of the length of the array safely?
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
Traversing an array means accessing each element one by one. In Java, arrays can be easily traversed using a loop where the loop variable runs from 0 to array.length - 1.
Published   December 21, 2016
🌐
Codecademy
codecademy.com › docs › java › arrays › .length
Java | Arrays | .length | Codecademy
April 21, 2025 - In Java, the .length property is used to determine the length or size of an array. It is a built-in property for arrays and returns an integer value that represents the number of elements in the array.
🌐
W3Schools
w3schools.com › java › ref_arrays_length.asp
Java Array length Property
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The length property returns the length of an array.
🌐
Codecademy
codecademy.com › forum_questions › 51e09679631fe98d15000f56
I don't understand how this works | Codecademy
now the if statement. Here, the part saying: array[i], this is actually referencing the value out of the array. the variable i stores a number from 0 to 7 (because it is going through how many items you have in the array, it goes until array.length) so you are saying
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-find-the-length-in-Java.php
How to Find the Length of a String or Array in Java
This finds the length of the string or array. To find the length of the string, you simply take the string in double quotes or the variable representing the string and follow it with a dot and the keyword length(). For a string, length must be length() and not just simply length.
🌐
TutorialB
tutorialb.com › post › 312
Java Array Length Working with Array Sizes in Java
Write the array name and then write the length followed by a dot ( . ). String[] favourite_languages = {"Java", "Kotlin", "C++", "Python", "JavaScript", "Php"}; System.out.println("The array favourite_languages length is = " + favourite_lan...
🌐
Coderanch
coderanch.com › t › 599411 › java › length-length
.length and .length() what (Beginning Java forum at Coderanch)
In the case of arrays, 'length' is a class variable that holds the number of indices in the array instance. In the case of Strings, length() is a method (not a variable) that returns the length of a String.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-array-size-explained-by-example
Java array size, length and loop examples
To find the size of a Java array, query an array’s length property. The Java array size is set permanently when the array is initialized.
🌐
Coderanch
coderanch.com › t › 398753 › java › array-length-property
array.length property (Beginning Java forum at Coderanch)
Beginning Java · rajaraman navaneethan · Ranch Hand · Posts: 86 · posted 20 years ago · Number of slices to send: Optional 'thank-you' note: Send · hi friends, can any one of you tell me where the array.length property is defined in java · Marilyn de Queiroz ·
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › array length in java
Java Array Length with Examples & Syntax
October 26, 2025 - Trying to access an element beyond the array’s length causes a java.lang.ArrayIndexOutOfBoundsException. Java arrays are zero-indexed, so valid indexes range from 0 to array.length - 1.