In Oracle Java Foundations 1Z0-811, exam objective 2.5 asks you to declare, initialise, and access elements of one-dimensional arrays. This concept solves the problem of needing to store multiple related pieces of data — like exam scores or customer names — without having to create a separate variable for each one. For you, passing this exam, understanding arrays is crucial because they appear in nearly every Java program and are a favourite topic for exam questions that test your ability to manage collections of data efficiently.
Jump to a section
A simple way to picture Introduction to Arrays
Have you ever wondered how your postal worker delivers mail to an entire apartment building without getting confused?
Think of an apartment building with numbered mailboxes in the lobby. The building itself is like a Java program, and the mailboxes are like an array. Each mailbox has a unique number, starting from 0 (the first one), 1 (the second one), and so on. In Java, those numbers are called 'indices' (or 'indexes') for the array. When you want to put something in a specific mailbox, you have to know its number — you cannot just say 'put this letter in the third mailbox', because the postal worker counts from 0. So the third mailbox is really mailbox number 2.
Now, when you build your apartment building, you decide how many mailboxes there are — 10, 20, or 100. In Java, when you declare an array, you tell it how many elements it can hold. Once built, you cannot add more mailboxes to the lobby wall. That is like an array's fixed length — once created, its size cannot change. If you want to put a letter in each slot, you must write the mail one by one for each mailbox, or you can use a loop to do it automatically. Arrays store related data together so you can access any piece quickly, just like reaching into the right mailbox by its number. That is the whole point: grouping items so you can get to any one of them instantly using a simple number.
An array in Java is like a container that holds a fixed number of values of the same type. Think of it as a row of identical boxes where each box can store one piece of information. The type might be int (whole numbers like 5 or 100), double (decimal numbers like 3.14), String (text like "hello"), or any other Java data type. The key rule is that all items in the array must be the same type — you cannot mix integers and text in one array.
Before you can use an array, you must declare it. Declaring means telling Java that you want an array and what type it will hold. The syntax looks like this: int[] scores; or String[] names;. Notice the square brackets [] after the data type — those brackets are the signal that this variable will be an array, not a simple single value. At this point, you have only declared the variable. The array does not exist yet in memory; you have just reserved the name.
To actually create the array, you must initialise it. Initialisation means setting aside memory for a specific number of elements. You do it like this: scores = new int[5];. This command says: 'Java, please create a container that can hold exactly five integers.' The word 'new' tells Java to allocate fresh memory. The number 5 inside the brackets is the length of the array — how many slots it has. Once the array is created, its length is fixed. You cannot change it later. If you need a bigger array, you must create a new one.
You can also declare and initialise in one line: int[] scores = new int[5];. Or you can use a shortcut with curly braces: int[] scores = {90, 85, 78, 92, 88};. This shortcut both creates the array and fills it with the values inside the braces. Java counts the values for you and sets the length automatically to 5.
Now, how do you put data into an array or get data out? You use an index. The index is the position number of an element, starting at 0. So the first element is at index 0, the second at index 1, the third at index 2, and so on. If your array has length 5, the valid indices are 0, 1, 2, 3, and 4. The last valid index is always length minus 1. To assign a value, you write: scores[0] = 90; scores[1] = 85; and so on. To read a value, you use the same syntax: int firstScore = scores[0];.
Why do arrays exist? Before arrays, if you wanted to store 100 exam scores, you would need 100 separate variables like score1, score2, score3... up to score100. That is impractical and messy. Arrays let you use a loop to process all 100 scores in just a few lines of code. For example, a for loop can go through each index and print the score, calculate an average, or find the highest value. This is called iterating over the array.
Here are the key characteristics of arrays beginners must know:
Arrays have a fixed length set at creation. You cannot add or remove slots later.
All elements must be the same data type.
Indexing starts at 0, not 1. This is a common source of errors.
Accessing an index that does not exist (like index 5 in a 5-element array) causes an ArrayIndexOutOfBoundsException, which crashes your program.
Arrays are objects in Java. They have a property called 'length' (without parentheses) that tells you how many elements they hold. For example, scores.length gives 5.
When you declare an array but do not initialise it, or when you initialise it without assigning values, Java assigns default values: 0 for numeric types, false for boolean, and null for objects like String.
To summarise the syntax for the exam:
Declaration: int[] myArray; Creation (initialisation): myArray = new int[10]; Both in one line: int[] myArray = new int[10]; Shortcut with values: int[] myArray = {1, 2, 3, 4, 5}; Access: myArray[2] = 42; or int x = myArray[2];
Remember, the exam loves to test whether you understand that array indices start at 0, that the last index is length-1, and that accessing an invalid index causes an exception. Practise writing small programs that create arrays, fill them with data using loops, and output the results. This will make the concept stick.
Declare an array variable
You tell Java you want an array by writing the data type followed by square brackets, then a name. Example: int[] scores;. This does not create the array yet — it only creates a variable that can hold a reference to an array.
Initialise the array with a specific length
Use the 'new' keyword followed by the data type and the desired length in brackets. Example: scores = new int[5];. This allocates memory for exactly 5 integers. The array now exists, and each element holds a default value (0 for int).
Assign values to individual elements
Use the array variable name, followed by the index in square brackets, then an equals sign and the value. Example: scores[0] = 85;. This puts the value 85 into the first slot. Repeat for each element you want to set.
Access a value from the array
To read a value, use the same bracket notation. Example: int firstScore = scores[0];. This copies the value at index 0 into the variable firstScore. You can then use it in calculations or print it.
Iterate over the array using a loop
To process every element without writing repetitive code, use a for loop that goes from 0 to array.length-1. Example: for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); }. This prints each element in order. The loop variable 'i' acts as the index.
Use the shortcut initialisation with values
If you know the values upfront, you can declare and initialise in one line using curly braces. Example: int[] scores = {85, 90, 78, 92};. Java automatically counts the values and sets the length to 4. This is faster than manually assigning each element.
Imagine you work for an IT company that processes customer orders. Every day, your system receives a list of order IDs — hundreds of them. Your manager asks you to write a Java program that takes these order IDs and finds the one with the highest value (the most expensive order). Without arrays, this would be a nightmare: you would have to create hundreds of separate variables. With arrays, it is straightforward.
Here is what happens step by step in a realistic business context:
The data arrives as a list of numbers from a database or a file. Your first task is to store these numbers in an array. You declare and initialise an array like double[] orderAmounts = new double[orderCount]; where orderCount is the number of orders.
You then fill the array by reading each order amount from the data source and assigning it to the correct index using a loop: for (int i = 0; i < orderCount; i++) { orderAmounts[i] = getNextAmount(); }. Here, 'i' acts as both the loop counter and the array index.
Once the array is full, you need to find the maximum value. You write another loop that goes through each index, compares the current element to a variable holding the highest value so far, and updates that variable if the current element is larger.
This same pattern appears everywhere: processing student grades, analysing sales figures, managing inventory quantities, or storing configuration settings. IT professionals also use arrays when working with graphical user interfaces — for example, storing a list of button labels or text field values.
Furthermore, when you join a bigger team, you will encounter arrays used in more complex structures. For instance, a web server might store incoming request IDs in an array to process them in order. A game developer might store the positions of all enemies in an array and update each one's position every frame by looping through the array.
The key takeaway for your career: mastering arrays means you can handle any situation where you have a collection of similar data. You will use them constantly alongside loops. The exam tests exactly this — they will give you a scenario and ask you to choose the correct way to declare, initialise, or access an array element. In your daily work, knowing how to manipulate arrays efficiently will save you hours of repetitive coding.
The Oracle Java Foundations 1Z0-811 exam tests arrays in a very specific way. Here is exactly what you need to know.
First, the exam expects you to recognise the correct syntax for declaring and initialising a one-dimensional array. They will give you multiple choices, and some will have subtle errors. For example, they might show int[] array = new int[]; without specifying a size — that is wrong because when you use 'new', you must provide the length. Or they might show int array[5]; which is incorrect Java syntax for declaration. The correct forms are int[] array; or int[] array = new int[5]; or int[] array = {1,2,3};.
Second, the exam loves testing array indices. Expect questions like: 'Given an array int[] nums = new int[10];, what is the value of nums[0]?' The correct answer is 0, because numeric arrays default to 0. A common trap: they might ask for the last valid index. For an array of length 10, the last index is 9, not 10. They will offer 10 as an option to catch people who forget that indices start at 0.
Third, they will test what happens when you access an invalid index. For example, 'What exception is thrown when you try to access array[5] on an array of length 5?' The answer is ArrayIndexOutOfBoundsException. Remember, valid indices are 0 through length-1. So for length 5, valid indices are 0,1,2,3,4. Index 5 is out of bounds.
Fourth, the exam will ask about array length. The property is 'length' — note: no parentheses, unlike the String class which uses length(). A common trap question gives you a String array and asks for its size: array.length returns the number of elements. Beginners sometimes confuse this with String.length() which returns the number of characters.
Fifth, they test the default values of uninitialised array elements. Memorise these: - int, double, short, long, byte, float: default is 0 - boolean: default is false - char: default is '\u0000' (the null character) - String or any object type: default is null
Sixth, the exam may present code with a loop that sums or averages array elements. They will ask you to determine the output. You must be comfortable tracing through a for loop that iterates from 0 to array.length-1 and accumulating values.
Seventh, watch for questions that ask you to identify the valid way to initialise an array with values using shortcut syntax: int[] arr = {1,2,3}; is correct, but int[] arr = new int[3]{1,2,3}; is invalid — you cannot specify both size and values in that form.
To prepare, practise with these exact scenarios. Use online Java compilers or IDE to write small programs and confirm your understanding. The more you practise, the more these patterns become automatic.
Array indices start at 0, so the first element is at position 0 and the last element is at position length-1.
An array's length is fixed once created — you cannot add or remove elements, only access or change existing ones.
All elements in a single array must be of the same data type (e.g., all int or all String).
Accessing an index outside the valid range (0 to length-1) throws an ArrayIndexOutOfBoundsException.
Uninitialised numeric array elements default to 0, boolean to false, and object references to null.
The array length property is accessed as array.length (no parentheses), not array.length() — that is for Strings.
These come up on the exam all the time. Here's how to tell them apart.
Array
Fixed length; cannot change size after creation
Elements accessed with numeric index starting at 0
Length obtained via array.length (field, no parentheses)
String
Immutable; cannot change characters, but can reassign variable
Characters accessed with charAt(index) method, not brackets
Length obtained via string.length() (method, with parentheses)
Array with default values
Created with 'new' and size: int[] a = new int[3];
All elements get default values (0, false, null)
You must assign each element individually later
Array initialised with values
Created with curly braces: int[] a = {1,2,3};
Values are set immediately
Length is determined by number of values provided
Valid array index
Ranges from 0 to array.length-1
Accessible without error
Example: array[0] for first element
Invalid array index
Any index less than 0 or greater than array.length-1
Causes ArrayIndexOutOfBoundsException
Example: array[array.length] causes error
int[] array
Preferred Java style: type and brackets together
Clearer that it is an array of integers
More readable in declarations like int[] a, b; (both arrays)
int array[]
C-style: brackets after variable name
Less common in modern Java
Can be confusing in multi-variable declarations: int a[], b; (only a is array)
Mistake
The first element of an array is at index 1.
Correct
The first element is at index 0. So if you have an array with 5 elements, the valid indices are 0, 1, 2, 3, and 4.
Many people come from everyday counting that starts at 1, so they naturally assume arrays work the same way. Java and most programming languages use zero-based indexing because it makes certain calculations simpler for the computer.
Mistake
You can change the size of an array after it is created by assigning a new value to its length property.
Correct
The length of an array is fixed at creation and cannot be changed. If you need a different size, you must create a new array and copy the elements.
The word 'length' sounds like it might be a settable attribute, but it is a read-only field. Beginners often try array.length = 10; which will cause a compilation error.
Mistake
If you declare an array but do not initialise it, you can still access its elements because Java will create a default array.
Correct
Declaring an array variable (e.g., int[] arr;) does not create an array object. You must initialise it with 'new' or with curly braces before you can use it. Otherwise, the variable is null, and accessing its elements causes a NullPointerException.
The concepts of declaration and initialisation are easily confused because in many contexts we use the words interchangeably. In Java, they are two distinct steps.
Mistake
An array can store elements of different data types.
Correct
All elements in an array must be the same data type. An int array can only hold integers, a String array only holds strings, and so on.
Real-life containers like a box might hold different items, so beginners expect programming containers to be flexible. Java enforces type safety to prevent errors at runtime.
Mistake
The length of an array is determined by the number of elements you have assigned to it, not by the size you declared.
Correct
The length is set at creation and does not change. If you create an array of size 10 but only assign values to the first 3 positions, the array still has length 10, and the other 7 positions hold default values.
People think of arrays like lists that grow as you add items. In Java, an array is a fixed-size container, not a resizable collection.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
It is based on how computer memory works: the index represents an offset from the starting memory address. The first element is at offset 0, so accessing it is fastest. This convention simplifies memory calculations for the compiler.
No, the length is fixed. If you need a different size, you must create a new array and copy the elements over using a loop or the System.arraycopy() method.
Java throws an ArrayIndexOutOfBoundsException at runtime. This stops your program. Always check that your index is between 0 and array.length-1 before accessing.
Use the array's length field: int size = myArray.length;. Note: no parentheses, unlike the String class which uses length().
No, all elements must be the same type. If you need to store mixed types, you would need to use a different structure like an ArrayList of Objects, but that is beyond this exam.
Both are valid syntax, but int[] array is the preferred style in Java because it clearly shows the type is an integer array. The int array[] style is inherited from C language and is less common.
You've finished Introduction to Arrays. Continue through the 1Z0-811 study guide to build a complete picture of the exam.
Done with this chapter?