Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
To create an array in Java, we must first need to declare a variable of the desired array type. The syntax for declaring an array is:
datatype[] arrayName;
where datatype is the type of data that array will hold, and
arrayName is the name of the array.
Once you have declared an array variable, you need to allocate memory for the array using the
new keyword. The syntax for allocating memory for an array is:
arrayName = new datatype[length];
where length is the number of elements that array will contain.
For example, the following code declares an array of integers named numbers with the length of 10:
int[] numbers = new int[10];
Once you have created an array, you have to initialize it with values. You can initialize array in two ways:
Directly: You can initialize it with some values when you declare the array. For example, the following code declares and initializes an array of integers named
numbers with the values 1, 2, 3, 4, and 5:
int[] numbers = {1, 2, 3, 4, 5};
Sequentially: You can initialize arrays sequentially by assigning a value to each element individually. For example, the following code initializes the array
numbers one by one:
After initializing the array, you can access its elements via index. The first element has index 0, the index of the second element is 1, and so on. For example, the following code prints the value of the first element of the array
numbers:
System.out.println(numbers[0]);
You can also iterate the array through the elements using a for loop. For example, the following code prints the values of all the elements of the array
numbers:
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Here are some other things to keep in mind about arrays in Java:
Arrays are objects in Java.
Arrays can be of any data type.
Arrays are always passed by reference in Java.
Arrays can be nested.
Arrays are dynamically allocated in Java.
Liked By
Write Answer
How can I create and work with arrays in Java?
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
06-Sep-2023To create an array in Java, we must first need to declare a variable of the desired array type. The syntax for declaring an array is:
where
datatype
is the type of data that array will hold, andarrayName
is the name of the array.Once you have declared an array variable, you need to allocate memory for the array using the
new
keyword. The syntax for allocating memory for an array is:where
length
is the number of elements that array will contain.For example, the following code declares an array of integers named
numbers
with the length of 10:Once you have created an array, you have to initialize it with values. You can initialize array in two ways:
numbers
with the values 1, 2, 3, 4, and 5:numbers
one by one:After initializing the array, you can access its elements via index. The first element has index 0, the index of the second element is 1, and so on. For example, the following code prints the value of the first element of the array
numbers
:You can also iterate the array through the elements using a for loop. For example, the following code prints the values of all the elements of the array
numbers
:Here are some other things to keep in mind about arrays in Java: