Arrays Part 1
1h 15m

In this course, we'll learn about Arrays in Java. 

Learning Objectives

  • What is an Array?
  • Declaration and Initialization
  • Sorting Arrays
  • Searching Arrays
  • Multi-Dimensional Arrays

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge

Hi there. In this section, you'll learn to work with arrays in Java. You'll learn how to declare, instantiate, initialize, and access array elements. An array is a container object that holds a fixed number of values of a single type. So, you can think of an array as a list of elements of the same type. For instance, you can create an array that can hold 200 values of int type. Okay. Let's look at how to declare an array in Java. The arrays syntax is dataType[] arrayName; or dataType arrayName[]; both are valid declarations but mostly, it's declared using the square bracket after the dataType. dataType can be a primitive data type like int, char, float, double, byte, etc. or it can be type string. arrayName is an identifier like a variable. In our example of array declaration, int[] age; means that age is an array that can hold int values. But how many elements can the array hold?

We defined it in the initialization part of the slide. We allocate memory for array elements. In our example, the length of the age array is 7 meaning that it can hold seven elements. And if you print the elements of this array to the console right now, you'll get all the elements as zero because it's an int array and the default value of an int data type is zero. If this were a string array of seven elements, this time, all elements would be null initially. The important point is after the creation of the array, its length is fixed so, it cannot be changed in the program. For example, we set the element number of this array to be seven, then, we cannot add the eighth element to this array. 

Each item in an array is called an element and each element is accessed by its numerical index. As shown in the slide, the index begins with 0. The fifth element for example, would therefore be accessed at index 4. Initializing Array Values. In Java, we can initialize arrays during declaration or we can initialize later in the program. In our example, this statement creates an age array and initializes it during declaration. The length of the array is determined by the numerical values provided which are separated by a comma. The length of the age array is seven. Also, we can easily access and change array elements by using numeric indexes. In our example, we change two values of the age array at index 0 and index 3 and new array values are located in the new area. Okay. Let's try to understand Java arrays with examples. 

In exercise project, right click on the source folder and select new class. Specify package name as array and class name as Array and select the checkbox to add the main method. Let's declare an int array that its name is numbers, int[] numbers; We have now declared an int array called numbers but we haven't initialized it yet. If we try to print the numbers array to the console as it is, we will get an error. Let's try. System.out.println(numbers);. As you can see, we're getting a compilation error. It says that the numbers array is not initialized. If you want, let's run the application and observe the error on the console. As you can see, it says that the numbers array is not initialized. Now, let's initialize it. 

Right after the array name i.e. numbers, I write = new int[]; I need to specify how many elements this array will consist of in the brackets. For example, let's write 4 inside the square brackets. This means that we have an array called numbers which contains four elements of data type int. Also, all elements are zero by default, as we are not currently specifying elements. Now, let's run the application once again. As you can see, we are no longer getting any errors. But if you notice, there is an expression on the console as if it were an ID number, let's look at this. The square brackets at the top indicate that it's an array. The letter I next to it indicates that this array is a type of int. In other numbers, it's a unique number created for this array. If you want, let's print the elements of the array now. 

For this, we can use the toString method of the arrays class. I write, (Arrays.toString(numbers)); Let's run the application again. As you can see, an array of four elements, each of which is 0, is printed to the console. Now, let's learn how to add elements to the array. Currently, the size of the numbers array is four. This means it can hold four int numbers. Let's assign values to elements of the array. The first element of the array and assign 4. The second element and assign 6.5. If you notice, we get a compilation error again because this is an int array and the elements of the array can only hold int values. That is, this array cannot contain a double or float or string. So, can we cast it? I'm typing (int) in parentheses here. 

Yes, we have blocked the compilation error now. Currently, we have assigned elements to indexes 0 and 1 of a four-element int array. Index 0 is 4. Index 1 has 6.5 converted into int data type. The second and third indexes are still 0. Let's run the application as it is once again. I'll move the print method here. I will cut these and paste them before the print method and test it again. As you can see, the first element is 4. The second element is 6 because we converted it into int data type. And the third and fourth elements are still 0 because we haven't assigned a value to them yet. Now, let's assign a value to all elements. The third element, assign -3. And the last element, assign -2. 

Yes, the array named numbers is no longer empty and each index has an element. Let's run the application once again. As you can see, we have successfully printed the elements of the array to the console. Yes, if you notice, we added the elements of the array later. So, can we also say initialize while declaring the array? Let's just do an example. Let's create a new array, int[] age = new int[3]; We have now declared that this array will hold three elements. If we want to add the index elements at this stage, we must open curly braces right after the square bracket and we have to delete the number of elements we declared here, otherwise, we will get a compilation error again. Now, let's determine the elements of the array. We can create them here by separating the elements of the array with commas. 

For example, let the first element be 15, the second element 20, and the third element 25. Now, let's print this array to the console. I'm copying the print method here and pasting it here. Also, the name of this array will be age. Let's run the application now. As you can see, we have successfully printed the elements of the array to the console. Yes, we can initialize the array another way. If we're going to declare the elements of the array at the declare stage, then we may not use the expression "new int[]" just to the right of the equal sign. We can just initialize the array using curly braces {}. Let's just do an example. I'm copying these two lines, and I'm pasting them here. Let the array name be age2. Now, let's delete the new int[] expressions here. Yes, as you can see, we did not encounter any compilation errors.

Now, let's print this array to the console. I'm changing this to age2. Let's run the application. As you can see, the elements of both arrays are successfully printed to the console. So, can we create an array without specifying the array elements and the number of elements? Let's just do an example. I'll copy and paste these two lines one more time. Let the name of this array be age3. Now, let's delete the elements of this array. Notice that we did not receive any compilation errors. Currently, this array is an empty array, it has no element and its length is zero. 

You can also write it like this. You can also type "new int[]" here before the curly brackets. This is also valid. The array is also an empty array at the moment. Now, let's print the array to the console. As you can see, there are only array symbols, no elements. So, can we add an element now? For example, let's create the first element; "age3 [0]" because the index number of the first element is [0] = 5. Let's run the application once again. As you can see, we got an error. If we want to add an index that is either negative or greater than or equal to the size of the array, an "ArrayIndexOutOfBoundsException" is thrown. 

It's thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program. Now, I want to draw your attention to another point. Let's create a new array first. This time let this array be of type string; string[] cars. At this point, I should also mention, you can also use the square bracket here after the name of the array. This is also valid, but the general usage is mostly after determining the type of the array. Let's continue now; "new string []".

Now, let's determine the number of elements of the array. I'm typing 3.5 here. As you can see, we got a compilation error because the number of elements of the array can only be an integer. So, can we write 7/2 instead of 3.5 here? Yes, we can write it this way because it will calculate the results of this operation as an int, not a double. So, right now, the number of elements of the array is actually three. So, if we define an array like this, we can only add three elements. You can also write it like this. Let's create two different types of int variables, int a = 7, int b = 2. Now, let's write a /b here. In this way, you can specify the number of elements of the array. But notice that the data type here is int. So, if we use double instead of int here, we will still get an error.

Even if the numbers here are divided by each other without a remainder, for example, even if the variable is an eight, we will still get an error. Of course, the following usage is also correct. Even if the variables are defined as double here, if each variable is converted to the int data type in square brackets, that is, if we write int in parentheses before a and b, this will also be valid. Because the error here is actually caused by the mismatch of the types. In other words, if you cast the number of elements of the array to the int data type, you will eliminate the error. Now, let's print the number of elements of this array; System.out.println ("The length of cars array is " + cars.length).

With length, we can learn the size of the array, that is, the number of elements. Now, let's run the application. As you can see, the size of the array, i.e., the number of elements is 4. Let's change the value of the a from 8 to 7 and test it again. As you can see, the size of the array, i.e., the number of elements is 3 this time. Shortly, the number of elements of the array must be an integer. Finally, I'd like to talk about one more topic and end the lesson. If you remember, we could define more than one variable on the same line. For example, we can define three data types of int data as follows; int x, y, z;. Does this rule apply to series? The answer is yes. Let's create an array now; String[] cars, books, plants. But since we created an array named cars above, this gives a compilation error, so this can be cars3. Okay. This means that we declared three arrays named cars, books and plants, and of type string. Of course, notice that the square bracket is used right after the data

type. If you are going to use it after the array name, this time you have to use it after each array name. So, if you just use square brackets after "cars", that means it creates an array named cars and two strings named books and plants. Note this usage as well. Yes, friends, let's take a short break here. We'll continue in the next video. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics