To explicitly declare an array in Java, you can use the new
operator and the array type
followed by square brackets.
Here's an example of how to explicitly declare an array of integers in Java:
int[] numbers = new int[5];
In the above example, the int[]
type specifies that the array will hold integers, and the new int[5]
expression creates an array with a length of 5.
You can also specify the initial values of the elements in the array using an array initializer. For example:
int[] numbers = new int[] {1, 2, 3, 4, 5};
This creates an array of integers with the values 1
, 2
, 3
, 4
, and 5
.
You can also use the shortcut syntax to declare and initialize an array in a single line:
int[] numbers = {1, 2, 3, 4, 5};
This creates an array of integers with the same values as the previous example.
Keep in mind that the size of an array is fixed and cannot be changed once it is created. To change the size of an array, you can create a new array with the desired size and copy the elements from the old array to the new one.