To initialize an array in Java, assign data in an array format to the new or empty array. Initializing an array in Java involves assigning values to a new array. Java arrays can be initialized during or after declaration.
In Java, arrays are used to store data of one single type. For instance, an array could store a list of the names of every employee that works with a company, or a list of bagel flavors sold at a local bakery.
Before you can start working with the array data type in Java, you first need to declare and initialize an array. In other words, you need to tell the program to create an array, and then add data to that array.
In this tutorial, we’ll discuss how to declare and initialize an array in Java. We’ll also walk through a few examples of initializing arrays.
Java Declare Array
Declaring an array is the process of telling a program that an array should exist. Before you can initialize an array and assign it values, you need to declare an array.
Here’s the syntax you should use to declare an array in Java:
dataType[] nameOfArray;
The syntax for declaring a Java array consists of the following components:
- dataType is the type of data the array’s values will store.
- [] indicates you are declaring an array.
- arrayName is the name of your new array.
So, suppose we want to declare an array called bagels
that stores a list of the bagel flavors sold at a local bakery. This array would contain string values. Here’s the code we would use to declare our array:
String[] bagelFlavors;
In this example, we have declared an array called bagelFlavors
which can hold String
values.
When you’re declaring an array, you may also want to define how many values the array can hold. Suppose we wanted our bagelFlavors
array to contain ten values. We could instruct our program to make room for ten values in our bagelFlavors
array using this code:
String[] bagelFlavors; bagelFlavors = new String[10];
On the first line, we declare our array. Then we use the new String[10]
syntax to tell our program that our array should hold ten elements. It’s important to note that once the array’s length has been defined, it cannot be changed.
Java Initialize Array
Initializing an array refers to the process of assigning values to an array. For instance, initializing an array of books would involve adding books to your array. Declaring an array, on the other hand, is where you tell a program that an array should exist.
There are two ways to initialize an array in Java: during declaration or after declaration.
Initialize During Declaration
In the previous examples, we demonstrated how to declare an array in Java without initializing its values. However, we can also create and initialize our array while declaring it. This is common if you already know the values you want to store in your array at the time of declaring the array.
Suppose we want to declare an array called bagelFlavors
and initialize it with five values. Here’s the code we would use to accomplish this task:
String[] bagelFlavors = {“Plain”, “Pumpernickel”, “Cinnamon-Raisin”, “Sesame”, “Egg”};
In this example, we have declared an array called bagelFlavors
and initialized the array with five values.
Initialize After Declaration
Alternatively, you can initialize an array after declaration. This is common in programs where you know you want to use an array to store a certain set of values but where you have not yet determined what those values should be.
Before you use this approach, you first need to declare an array. So, if we wanted to declare an empty array called bagelFlavors
, we would use the code like we did above:
String[] bagelFlavors;
Now we have declared our array, we can initialize its values. We can do so by assigning the values we want our array to have to the bagelFlavors
variable, just like we would when assigning any value to a variable. Here’s the code we would use:
bagelFlavors = new String[] {“Plain”, “Pumpernickel”, “Cinnamon-Raisin”, “Sesame”, “Egg”};
In the code above, we initialize our variable bagelFlavors
with five values.
Accessing Array Elements
So far, we have declared an array of bagel flavors and initialized it with some values. Now that we have our arrays ready, we can start accessing the elements in our array.
In Java, items in an array are assigned index values starting from 0 and going up through the length of our array, or the number of elements in our array. These index numbers are used to access an individual item in an array. Here are the index number assigned to our bagelFlavors
array from earlier:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Plain | Pumpernickel | Cinnamon-Raisin | Sesame | Egg |
0 | 1 | 2 | 3 | 4 |
Suppose we wanted to retrieve the item at the index value 1 in our array. We could do so using this code:
class RetrieveBagel { public static void main(String[] args) { String[] bagelFlavors = {"Plain", "Pumpernickel", "Cinnamon-Raisin", "Sesame", "Egg"}; System.out.println(bagelFlavors[1]); } }
In our code, we define a class called RetrieveBagel
, which stores our code for the program. Then we declare and initialize an array called bagelFlavors
which stores the list of bagel flavors sold at our local bakery. Then we print out the value with the index number 1 in the bagelFlavors
array.
Our code returns the item at the index value 1, which is as follows:
Pumpernickel
In the same way, we could access the element at index 0 to get ‘Plain,’ or the element at index 3 and get ‘Sesame.’
Conclusion
In Java, there are two ways to initialize an array: during declaration and after declaration. Typically, you declare and initialize an array at the same time if you know the values you want your array to contain at the time of declaration; otherwise, you initialize an array after declaration.
This tutorial discussed how to declare and initialize an array in Java, with reference to examples. In addition, this tutorial explored how to access individual items from a Java array.
Now you have the skills you need to initialize Java arrays like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.