Array Lengths in Java

Instructor: Thomas Wall

Thomas is a professional software developer, online instructor, consultant and has a Masters degree.

You'll learn how Java represents and stores arrays of data, how to access array elements and determine the number of elements (i.e., the length) of an array.

Line 'em up!

We're all familiar (hopefully not from direct experience) with a suspect lineup:

Java Array Lineup

The suspects have a fixed position and are referenced by their position in the line. A Java array is an ordered set of data values all of the same type and designated by a name (a.k.a. identifier) chosen by the programmer. However, in Java (as in most other programming languages), the positions are designated beginning with 0 (instead of 1):

Java Array Lineup

Java Array Declaration

The data type of all Java variables, including arrays, must be declared before they can be used. An array declaration follows the general form of all Java declarations, but the special '[]' (square brackets) designation is placed after the data type to indicate the identifier represents an array of values (as opposed to a single value). Here are some examples of array declarations:

Java Array Declarations

Running the program produces this output:

ages[0] = 21

ages[1] = 0

ages[2] = 18

ages[3] = 0

ages[4] = 19

Once storage is allocated to hold the elements of a Java array (in this case with the 'new int[5]' statement), the number of elements in the named array is fixed and can't be changed. As our example shows, the 'ages' array contains 5 elements whether we explicitly store values in them or not. The number of elements allocated for a Java array can be determined via the length property. In our example, 'ages.length' has the value of 5. Array elements can also be initialized with values at declaration time through the use of '=' followed by a comma separated list of values enclosed in curly brackets ('{...}'). For example:

Java Declaration Example

Running the program produces this output:

primes.length = 6

names.length = 3

primes[0] = 2

primes[5] = 13

names[0] = Bob

names[2] = Ted

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account